0

我正在使用 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,所以我不确定问题是什么。

4

2 回答 2

1

如果你告诉 jQuery 期待 JSONP,你必须发送JSONP [Wikipedia],而不是 JSON。

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

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


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

于 2012-04-24T19:39:05.060 回答
-1

您的 JSON 无效。我数了 7 个左大括号,只有三个右大括号。此外,括号的数量不会相加。

于 2012-04-24T19:31:31.783 回答