我在如何处理空值方面遇到困难。每当我将 JSON 发送到服务器时,前端中的所有 null
值都会变成 "null"
作为字符串。我该如何处理这个问题?
AngularJS: 在我的 Controller 中。我正在使用 ng-file-upload 插件 https://github.com/danialfarid/ng-file-upload/wiki/PHP-Example 从表单发送值
$scope.saveNewDocument = function(){
var formAttachments = Upload.upload({
url: appServer.baseURL + appServer.api.generalDocuments,
method: 'POST',
file: $scope.formData.formAttachments,
sendFieldsAs: 'form',
fields: $scope.formData // $scope.formData is ng-model in view forms
}).then(function(success){
//some codes
}, function(error){
console.log(error);
});
};
Laravel:在我的 Controller 中。
public function store(Request $request)
{
$request = $request->all();
//if I get values from request. All null becomes "null"
}
最佳答案
由于您没有提供任何代码,我不确定您的问题到底是什么,但假设在 PHP 中解决这个问题(这被标记为),您可以编写一个简单的 if 语句并在发送之前检查数据如下:
if($var==null)
$var=0;
关于javascript - AngularJS(前端)和 Laravel(后端)中 JSON/Arrays 中的 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521125/