VM2022:2 未捕获的语法错误:意外的令牌:在 AJAX 调用中

问题描述 投票:0回答:1

我从后端发送 GET 请求来获取 json 响应。我收到这个错误。

未捕获的语法错误:意外的标记:
在 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)

enter image description here

我的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
});

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

javascript python jquery json ajax
1个回答
5
投票

你有两个问题。首先,响应已由 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
});
© www.soinside.com 2019 - 2024. All rights reserved.