AJAX调用和清理JSON但语法错误:缺失; 在声明之前

JZw*_*ige 42 javascript ajax jquery json jsonp

我正在使用以下代码进行跨域JSONP调用:

jQuery.ajax({
        async: true,
        url: 'http://mnews.hostoi.com/test.json',
        dataType: 'jsonp',
        method: "GET",
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(textStatus + ': ' + errorThrown);
        },
        success: function (data, textStatus, jqXHR) {
            if (data.Error || data.Response) {
                exists = 0;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

在Firebug中调试时,我收到以下错误:

在此输入图像描述

SyntaxError: missing ; before statement
Run Code Online (Sandbox Code Playgroud)

但是,当我通过像jsonlint.com这样的工具传递我的json对象(通过JQ代码中的链接可用)时,它说它是有效的JSON.我也没有发现任何异常现象.怎么会返回语法错误?它是一些JSONP细节我没有得到或什么?

JSON示例

{"news":[ {
  "sentences": [
    "Neuroscientists have discovered abnormal neural activity...", 
    "The researchers found that these mice showed many symptoms...", 
    "\"Therefore,\" the study authors say, \"our findings provide a novel.."
  ], 
  "summaryId": "ZJEmY5", 
  "title": "Abnormal neural activity linked to schizophrenia"
}]}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Que*_*tin 29

JSONP不是JSON.JSONP响应将包含一个JavaScript脚本,该脚本仅包含一个带有一个参数的函数调用(到预定义函数)(这是一个符合JSON语法的JavaScript对象文字).

您获得的响应是​​JSON,而不是JSONP,因此您作为JSONP处理它的努力失败了.

更改dataType: 'jsonp'dataType: 'json'(或完全删除该行,服务器会发出正确的内容类型,因此您无需覆盖它).

由于您的脚本运行在与JSON不同的源上,因此您还需要采取步骤(大多数(但不是全部),这些步骤要求您控制提供JSON的主机)以解决相同的源策略.


epa*_*llo 7

该错误是因为它返回JSON而不是JSONP.

JSONP应该看起来像

someCallBackString({ The Object });
Run Code Online (Sandbox Code Playgroud)