javascript - Angular ajax : Unexpected token I in JSON at position 0

标签 javascript angularjs json ajax

我正在尝试在 http post 请求 中将 html 作为 json 发送。但是当我尝试发出 $http.post 请求时,出现以下错误:

angular.js:12783 SyntaxError: Unexpected token I in JSON at position 0
    at Object.parse (native)
    at fromJson (http://localhost:9001/bower_components/angular/angular.js:1274:14)
    at defaultHttpResponseTransform (http://localhost:9001/bower_components/angular/angular.js:9703:16)
    at http://localhost:9001/bower_components/angular/angular.js:9794:12
    at forEach (http://localhost:9001/bower_components/angular/angular.js:341:20)
    at transformData (http://localhost:9001/bower_components/angular/angular.js:9793:3)
    at transformResponse (http://localhost:9001/bower_components/angular/angular.js:10582:21)
    at processQueue (http://localhost:9001/bower_components/angular/angular.js:15097:28)
    at http://localhost:9001/bower_components/angular/angular.js:15113:27
    at Scope.$eval (http://localhost:9001/bower_components/angular/angular.js:16359:28)

我的代码( Angular )如下:

  $scope.generate_pdf = function() {
    var html =  angular.element('html').html(); // get all html

    var service = API.getService();
    // JSON.stringify( { html:  html } this also cause same error
    service.downloadPdf({}, { html: html },
      function(res) {
        console.log("res : ", res);
      }, function(err) {
        console.log("err : ", err);
      });


  };

我该如何解决这个问题?提前致谢

最佳答案

SyntaxError: Unexpected token I in JSON at position 0 表示服务器响应不是有效的 JSON,请打开控制台中的网络选项卡并检查请求的响应以查看问题所在。

关于javascript - Angular ajax : Unexpected token I in JSON at position 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37593775/

相关文章:

json - 将XmlSlurper GPathResult转换为JSON字符串

javascript - 选中复选框时使 div 可见的附加 Javascript 代码

javascript - 如何使用 Angular 更新绑定(bind)到函数的元素?

javascript - split 不是数组上的函数

asp.net - 将 Angular SpaTemplate Core 转换为 Core .Net Framework

javascript - 我应该如何从 Node Js 中的 Url 中提取值

java - JSON 反序列化属性,包括 $

python - 使用用户定义的类作为值序列化 Python 中的字典 (Flask)

javascript - 如何在具有相同类名的所有元素上调用 javascript 函数

javascript - Ajax 变量重用构建