AJAX调用和干净的JSON,但语法错误:缺少;语句之前 [英] AJAX call and clean JSON but Syntax Error: missing ; before statement

查看:499
本文介绍了AJAX调用和干净的JSON,但语法错误:缺少;语句之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code使跨域JSONP电话:

I am making a cross domain JSONP call using this code:

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

当在Firebug的调试,我得到以下错误:

When debugging in Firebug, I get the following error:

SyntaxError: missing ; before statement

然而,当我通过像jsonlint.com工具通过我的JSON对象(可通过在JQ code中的链接),它说,它是有效的JSON。而且我不觉得任何异常无论是。怎么可能被返回一个语法错误?它是一些JSONP细节我没有得到还是什么?

However, when I pass my json object (available through the link in the JQ code) through a tool like jsonlint.com, it says it is valid JSON. And I don't find any anomalies either. How could it be returning a syntax error? Is it some JSONP detail I am not getting or what?

{"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"
}]}

在此先感谢。

Thanks in advance.

推荐答案

JSONP不是JSON。一个JSONP响应将包括一个只包含一个函数调用(到pre定义的函数)的一个参数(这是一个JavaScript对象常量符合要求的,以JSON语法)一个JavaScript脚本。

JSONP is not JSON. A JSONP response would consist of a JavaScript script containing only a function call (to a pre-defined function) with one argument (which is a JavaScript object literal conforming to JSON syntax).

你得到的响应是JSON,不是JSONP所以你的努力来处理它作为JSONP失败。

The response you are getting is JSON, not JSONP so your efforts to handle it as JSONP fail.

修改数据类型:JSONP数据类型:JSON(或者完全删除行,服务器问题正确的内容类型,所以你并不需要覆盖它)。

Change dataType: 'jsonp' to dataType: 'json' (or remove the line entirely, the server issues the correct content-type so you don't need to override it).

由于你的脚本是在不同的产地到JSON运行,那么你还需要的采取措施(大部分,但不是全部,这些都需要你控制服务的JSON主机)来解决的<一个href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript">same来源政策。

Since your script is running on a different origin to the JSON then you will also need to take steps (most, but not all, of which require that you control the host serving the JSON) to work around the same origin policy.

这篇关于AJAX调用和干净的JSON,但语法错误:缺少;语句之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆