繁体   English   中英

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

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

我正在使用Jquery来获取json文件,并且在浏览器中遇到错误(意外令牌:)

这是我的JavaScript:

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

            }
        }); 
    },

这是我的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"
                    }
                  ]
                },

我使用JSONLint验证了JSON,所以我不确定是什么问题。

如果您告诉jQuery需要JSONP,则必须发送JSONP [Wikipedia]而不是JSON。

即响应将是someFunctionName({...json here...}); 其中someFunctionName通常是随URL发送的callback参数的值(由jQuery自动生成)。

jQuery将响应评估为JavaScript,这会在您的情况下产生错误,因为纯JSON是无效的JavaScript。


或者,如果它实际上不是跨域请求( xml/scenes.json是“本地” URL), xml/scenes.json dataType: "jsonp"更改为dataType: "json" (并删除crossDomain )。

您的JSON无效。 我计算的是7个大括号,只有3个大括号。 另外,括号的数量也不累加。

暂无
暂无

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

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