【问题标题】:POST error 500 (Internal Server Error) - JSON syntax errorPOST 错误 500(内部服务器错误)- JSON 语法错误
【发布时间】: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


【解决方案1】:

我猜错误 html 不是由您的代码生成的,而是由主机(如 IIS)生成的。使用 json 数据创建错误响应可能是一种更好的做法。 无论如何,你可以在angularJs中分别处理ok和error响应。

$http.post返回一个promise对象,可以这样使用

$http.post(url, data)
.then(function(response){
   //handle normal result data here 
})
.catch(function(response){
  //handle error case here 
})

【讨论】:

  • 代码中已经有成功和错误响应。我已经编辑了问题以包含一些代码
  • @Clint_A,看看你的更新,也许你可以检查你是否在errorFunction中将html解析为json
【解决方案2】:

它没有完全完成的问题。您必须提及您尝试解析的数据及其响应。

您收到 500 错误,似乎您从服务器收到错误。 这意味着您的后端代码制作错误。

另外请检查您的解析数据类型。

我想你必须提到responseType"json"

   this.AjaxPost2 = function (data, route, successFunction, errorFunction, extras) 
   {
    $http({
        method: 'POST',
        url: route,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        data: data,
       responseType: 'json',
    }).success(function (response, status, headers, config) {
        successFunction(response, status, extras);
    }).error(function (response) {
        errorFunction(response);
    });

}

您必须提及您正在解析哪些数据。

【讨论】:

    【解决方案3】:

    一个很晚的答案,但我了解到此类错误几乎总是由于 api 本身的错误。语法错误大部分。前端期望返回 json,但 api 遇到了故障,因此它从未运行或返回其他内容。

    解决此问题的一种快速方法是在 REST 客户端(如 postman 或 insomnia)中运行 api,查看返回的内容并进行必要的修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-28
      • 2018-01-06
      • 1970-01-01
      • 2019-12-05
      • 2017-09-03
      • 2018-10-10
      • 2014-01-20
      • 2014-09-24
      相关资源
      最近更新 更多