0

我遇到了一个小问题。我正在尝试使用$.ajax我在网上仔细检查过的 jQuery 调用访问 JSON 文件,并且我的 JSON 代码是有效的。当我进行调用时,它会在 JSON 中引发带有语法错误的解析错误。

您可以通过访问http://michael-nolan.com/找到错误

这是我的javascript:

$(document).ready(function()
{
    $.ajax(
        { type: "GET",
          url: 'projects/projects.json', 
          dataType: "json", 
        success: function(results)
        { 
            console.log("Success!"); 
        }, 
        error: function(XMLHttpRequest, textStatus, errorThrown)
        { 
            console.log(textStatus); console.log(errorThrown); 
        } 
    });
});

这是我的 JSON

{
    "projects":
    [
        {
            "title":"Adobe Suite",
            "description":"Some stuff",
            "imgsrc":"img/adobe_suite_description.png"
        },
        {
            "title":"Gridlock",
            "description":"Stuff",
            "imgsrc":"img/gridlock_description.png"
        },
        {
            "title":"Open Cart",
            "description":"more stuff",
            "imgsrc":"img/opencart_description.png"
        }
    ]
}
4

7 回答 7

2

在 Chrome 中运行良好,IE 对 JSON(所有事物)特别严格。

在你的第二和第三个描述中寻找换行符。我怀疑这就是 IE 失败的原因。

于 2013-02-28T05:31:42.430 回答
1

该错误与第二个数组条目中两个段落之间的空格(可能是换行符)有关

...in the game.</p>
    <p>Gridlock...
^
|
the problem
于 2013-02-28T05:32:32.653 回答
0

当我测试它时,您的代码运行良好。根据语法错误是什么,我可能会检查 json 是如何生成的,以及导航到 projects/projects.json 是否正确生成了 json。如果生成了 json,尝试用静态 json 替换它,看看是否有效。

于 2013-02-28T05:30:47.180 回答
0

JSON 中有制表符和换行符,这会导致解析器失败。将每个选项卡实例替换为\t并将换行符替换\n为例如:

示例 JSON 文件:

{
    textWithTabs : "This is text with \t a tab and \n newline"
}

JavaScript:

var o = JSON.parse(json);

console.log(o.textWithTabs); //This is text with     a tab and ↵ newline
于 2013-02-28T05:38:14.067 回答
0

我已经测试了你的代码及其对我的工作,我也在你的网站上尝试过它并给出了304 not modified.

所以尝试使用getJson()jQuery方法。

$.getJSON('projects/projects.json', function(data) {

});
于 2013-02-28T05:28:08.287 回答
0

我建议你试试这个:

{
"projects": [
    {
        "title": "Adobe Suite",
        "description": "<p>With years of experience using the Adobe Suite products I have skills that range from creating vector artwork in Adobe Illustrator, arranging web layouts in Adobe Photoshop, and creating print layouts in Adobe InDesign</p>",
        "imgsrc": "img/adobe_suite_description.png"
    },
    {
        "title": "Gridlock",
        "description": "<p>In my Sophomore year in college I worked with Jayson Fitch as an artist on a isometric 2d shooter called Gridlock. We as a team created all of the art assets that are being used in the game.</p><p> Gridlock is still in development and we hope to release it on the OUYA.Check out it's development blog here. <a href='http://gridlock-game.tumblr.com'>www.Gridlock-Game.Tumblr.com</a></p>",
        "imgsrc": "img/gridlock_description.png"
    },
    {
        "title": "Open Cart",
        "description": "<p>As a freelance project I initiated an overhaul of the Legendary Realms Terrain e-commerce storefront. This entailed a complete visual re-branding as well as creating a backend solution to expand online payment options.</p><p> Feel free to check them out at < a href = 'lrterrain.com' > Legendary Realms Terrain < /a></p > ",
        "imgsrc": "img/opencart_description.png"
    }
  ]
}
于 2013-02-28T05:48:38.770 回答
-1

记录的错误听起来像是您的 json 的问题。尝试在你的 json 中切换[ ] for a 。{}

于 2013-02-28T05:25:56.377 回答