简体   繁体   English

jQuery / Ajax在JSON请求中出现意外令牌

[英]unexpected token in json request with jquery / ajax

I'm using Jquery to fetch a json file, and I'm getting errors in the browser (Unexpected token : ) 我正在使用Jquery来获取json文件,并且在浏览器中遇到错误(意外令牌:)

Here's my javascript: 这是我的JavaScript:

fetchScenes: function() {
        $j.ajax({
            crossDomain:true,
            url: 'xml/scenes.json',
            dataType: "jsonp",
            success: function( sceneXML ) {

            }
        }); 
    },

Here's a partial structure from my json: 这是我的json的部分结构:

{ "scenes": {
    "scene": {
      "-id": "0",
      "-name": "Master",
      "scene": [
        {
          "-id": "1",
          "-name": "Weekday Vehicle",
          "-description": "Your home away from home.",
          "scene": [
            {
              "-id": "10",
              "-name": "For My Commute",
              "scene": [
                {
                  "-id": "13",
                  "-name": "Heading to School",
                  "scene": [
                    {
                      "-id": "20",
                      "-name": "Style Seeker",
                      "-vehicles": "1,3,6"
                    },
                    {
                      "-id": "21",
                      "-name": "Creative Thinker",
                      "-vehicles": "9,0,3"
                    }
                  ]
                },

I validated the JSON with JSONLint, so I'm not sure what the problem is. 我使用JSONLint验证了JSON,所以我不确定是什么问题。

If you tell jQuery to expect JSONP, you have to send JSONP [Wikipedia] , not JSON. 如果您告诉jQuery需要JSONP,则必须发送JSONP [Wikipedia]而不是JSON。

Ie the response would be someFunctionName({...json here...}); 即响应将是someFunctionName({...json here...}); where someFunctionName is usually the value of the callback parameter sent with the URL (generated by jQuery automatically). 其中someFunctionName通常是随URL发送的callback参数的值(由jQuery自动生成)。

jQuery is evaluating the response as JavaScript, which generates an error in your case, since plain JSON is invalid JavaScript. jQuery将响应评估为JavaScript,这会在您的情况下产生错误,因为纯JSON是无效的JavaScript。


Or if it is actually not a cross-domain request ( xml/scenes.json is a "local" URL), change dataType: "jsonp" to dataType: "json" (and remove crossDomain ). 或者,如果它实际上不是跨域请求( xml/scenes.json是“本地” URL), xml/scenes.json dataType: "jsonp"更改为dataType: "json" (并删除crossDomain )。

Your JSON is invalid. 您的JSON无效。 I'm counting 7 opening curly braces and only three closing braces. 我计算的是7个大括号,只有3个大括号。 Also, the number of brackets doesn't add up. 另外,括号的数量也不累加。

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

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