【问题标题】:VM2022:2 Uncaught SyntaxError: Unexpected token : on AJAX callVM2022:2 Uncaught SyntaxError: Unexpected token : on AJAX call
【发布时间】:2018-11-13 10:58:41
【问题描述】:

我从后端发送一个 GET 请求以获取 json 响应。我得到了这个错误。

Uncaught SyntaxError: Unexpected token :
在 DOMEval (jquery-3.3.1.js:111)
在 Function.globalEval (jquery-3.3.1.js:345)
在文本脚本 (jquery-3.3.1.js:9640)
在 ajaxConvert (jquery-3.3.1.js:8787)
完成后 (jquery-3.3.1.js:9255)
在 XMLHttpRequest。 (jquery-3.3.1.js:9548)

我的 AJAX 请求代码是:

$.ajax({
  type: "GET", //rest Type
  dataType: 'jsonp', //mispelled
  url: "{{ url_for('live') }}",
  contentType: "application/json",
  success: function (msg) {
    console.log(msg);
    for (var i = 0; i < msg.counters.length; i++) {
      var counter = msg.counters[i];
      console.log(counter);
    }
  }, 
  error: ErrorMsg
});

我不知道我哪里出错了。请帮忙。

【问题讨论】:

  • msg 已经是一个对象,因此再次对其进行解码将导致您看到的错误。您只需要删除var msg = JSON.decode(msg);
  • @RoryMcCrossan 仍然收到错误
  • 是不是同样的错误?
  • @RoryMcCrossan 是同样的错误。 VM2335:2 Uncaught SyntaxError: Unexpected token :
  • 很高兴你能成功。我在下面为您添加了答案。

标签: javascript python jquery json ajax


【解决方案1】:

你有两个问题。首先,响应已经被反序列化以供您购买 jQuery。再次反序列化它会导致您看到的错误。其次,响应格式似乎是 JSON,而不是 JSONP,因此还需要修改 dataType 属性。试试这个:

$.ajax({
  type: "GET",
  dataType: 'json',
  url: "{{ url_for('live') }}",
  contentType: "application/json",
  success: function (msg) {
    for (var i = 0; i < msg.counters.length; i++) {
      var counter = msg.counters[i];
      console.log(counter);
    }
  }, 
  error: ErrorMsg
});

【讨论】:

  • 我一直在寻找确切的问题,终于在这里找到了,我从未失去对 stackoverflow 的信任:P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 2013-11-03
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多