简体   繁体   English

未捕获到的SyntaxError:意外的令牌:返回JSON时

[英]Uncaught SyntaxError: Unexpected token : when returning JSON

I've got some jQuery that is successful in hitting an endpoint, here's my code 我有一些可以成功击中端点的jQuery,这是我的代码

$.ajax({
    dataType: "json",
    type: "GET",
    url: "https://url.that-works.com/env/Development/GetDevs/",
    username: "username",
    password: "password",
    data: "callback=?"
})
.done(function (data) {
    alert("success");
})
;

The code returns the following JSON response - which I can see in Chrome Developer Tools: 该代码返回以下JSON响应-我可以在Chrome开发者工具中看到该响应:

{
"developments": [
    {
        "name": "h2010 Ph2"
    },
    {
        "name": "The Meadows Ph2"
    },
    {
        "name": "h2010 Ph3"
    },
    {
        "name": "h2010 Ph4"
    },
    {
        "name": "The Meadows Ph3"
    }
  ]
}

I've checked that this is valid JSON being returned, but I keep getting an error message: Uncaught SyntaxError: Unexpected token : 我检查了这是返回的有效JSON,但我不断收到错误消息: Uncaught SyntaxError:Unexpected token:

I'm not sure what exactly I'm doing wrong here or why I'm getting this message, any guidance would be really appreciated. 我不确定在这里我到底在做什么错,或者为什么我收到此消息,所以不胜感激。

bengrah. bengrah。

In jQuery docs you can find the following: 在jQuery文档中,您可以找到以下内容:

'Cross-domain json requests are converted to jsonp unless the request includes jsonp: false in its request options.' “跨域json请求将转换为jsonp除非请求的请求选项中包含jsonp: false 。”

So probably you are making a cross domain call and it is converted to jsonp because you have specified dataType: 'json' . 因此,可能您正在进行跨域调用,并且将其转换为jsonp因为您已指定dataType: 'json'

When in JSONP mode, jQuery looks for such kind of response: 在JSONP模式下,jQuery查找这种响应:

jQuery18406445654265099913_1319844792316({
"developments": [
    {
        "name": "h2010 Ph2"
    },
    {
        "name": "The Meadows Ph2"
    },
    {
        "name": "h2010 Ph3"
    },
    {
        "name": "h2010 Ph4"
    },
    {
        "name": "The Meadows Ph3"
    }
  ]
}) 

but not a JSON string, so you are getting an error during the convertion. 但不是JSON字符串,因此在转换过程中会出现错误。 JSONP expects a javascript, not a string. JSONP需要JavaScript,而不是字符串。 If you take a look at the jQuery code you can make it more clear for you, but that's in general. 如果您看一下jQuery代码,就可以使它更清晰,但这很一般。

Eventually figured out the answer. 最终找到答案。

In our case, we were using a middleware appliance that generates the above URL/orchestration the script hits to get the JSON and so on. 在本例中,我们使用的是中间件设备,该设备生成脚本所命中的上述URL /业务流程以获取JSON等。 What we were trying to do was use a JSONP request to get at this info, but the orchestration returns JSON that wasn't wrapped in a function , which is where our problem lies. 我们试图做的是使用JSONP请求获取此信息,但是业务流程返回的是未包装在function中的 JSON,这就是我们的问题所在。

Instead, we changed it to a standard JSON get request, which then caused the Access-Control-Allow-Origins error (good link here ). 相反,我们将其更改为标准的JSON get请求,这随后导致Access-Control-Allow-Origins错误此处是好的链接)。 At this point, we added the Access-Control-Allow-Origins setting to the orchestration/server side - then we were able to get the JSON back in a usable format successfully. 此时,我们在业务流程/服务器端添加了Access-Control-Allow-Origins设置-然后我们能够成功以可用格式返回JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM