【发布时间】:2018-06-04 00:59:02
【问题描述】:
我有一个 AngularJS 1.5 应用程序,其表单将数据发布到数据库。在表单中有一个下拉选择。问题是,下拉 POST 中的某些选项成功,而其他选项则遇到 POST 错误说
POST http://localhost/myURL 500 (Internal Server Error)
在它下面……
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.qc [as fromJson] (angular.js:1285)
at apiError (postFormCtrl.js:957)
at AjaxServices.js:37
at angular.js:11029
at angular.js:15616
at m.$eval (angular.js:16884)
at m.$digest (angular.js:16700)
at m.$apply (angular.js:16992)
at g (angular.js:11313)
什么可能导致同一表单中的某些项目成功 POST 而其他项目遇到此错误?我已经仔细检查了缺少逗号、括号等的代码......没有。
我检查了浏览器上的输出,成功帖子的响应标头有
Content-Type:application/json 在响应头中,而失败的 POST 有
Content-Type:text/html 在响应标头中。会不会是这个问题?
如果是,我该如何防止它,因为我在应用程序中没有将内容类型设置为 text/html 的单一设置。
此外,我知道问题不可能出在 ajax post 函数中,因为整个应用程序使用相同的 ajax post 函数并且它们运行良好。
更多信息
这是下拉选择中的项目示例:
<div class="row" ng-if="isStudentActivity">
<div class="col-sm-10">
<label for="activity">Select an activity:</label>
<div>
<ui-select ng-model="theactivity.selected" theme="select2" class="form-control" name="activity" required>
<ui-select-match placeholder="Select or search an activity...">
<span>{{$select.selected.activity}}</span>
</ui-select-match>
<ui-select-choices repeat="item in activities | filter: $select.search">
<span ng-bind-html="item.activity | highlight: $select.search"></span>
</ui-select-choices>
</ui-select>
<p ng-show="dataFilterForm.activity.$invalid && (!dataFilterForm.activity.$pristine || dataFilterForm.$submitted)" class="help-block"><i class="fa fa-exclamation-triangle"></i> You must choose an activity.</p>
</div>
</div>
</div>
这是应用程序中所有 POST 使用的 ajax 函数
this.AjaxPost2 = function (data, route, successFunction, errorFunction, extras)
{
$http({
method: 'POST',
url: route,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: data
}).success(function (response, status, headers, config) {
successFunction(response, status, extras);
}).error(function (response) {
errorFunction(response);
});
}
并且该函数被调用使用
this.addCommunication = function (request, successFunction, errorFunction, params) {
ajaxService.AjaxPost2(request, path + "/addCommunication", successFunction, errorFunction, params);
};
【问题讨论】:
-
你在发什么? JSON 对象,字符串,还有什么?
-
你应该显示一些代码,否则我们只能猜测。
-
你的后端有错误(PHP?)检查你是如何处理你的数据的
-
@AlekseySolovey 你确定吗?如果他发布 JSON 对象而服务器需要字符串怎么办?
-
根据我检查的数据,它发布 JSON 对象
标签: javascript jquery angularjs json post