Uncaught SyntaxErrror:chrome中的意外标记ILLEGAL

时间:2023-01-30 22:45:38

I'm having a problem within the jQuery ajax function. The following code works fine in Mozilla but doesn't work in IE or Chrome, when I try to identify the problem using Developer Tools in chrome I get the error:

我在jQuery ajax函数中遇到了问题。以下代码在Mozilla中工作正常,但在IE或Chrome中不起作用,当我尝试使用chrome中的Developer Tools识别问题时,我得到错误:

Uncaught SyntaxError: Unexpected token ILLEGAL \n
$.ajax.success

and when I click on it it directs me to the var obj = JSON.parse(data); line.

当我点击它时它会将我引导到var obj = JSON.parse(data);线。

function getdata(){
    $.ajax({
        type:"GET",
        url: "https://gdata.youtube.com/feeds/api/users/TheSyndicateProject/playlists?v=2&alt=jsonc",
        data: "",
        success: function(data) {
            var obj = JSON.parse(data);
            displayPlaylists(obj);
        }

    });
}

EDIT: Ive found a solution that works in chrome but still not in IE

编辑:我发现了一个解决方案,适用于chrome但仍然不在IE中

function getdata(){
        $.ajax({
                type:"GET",
                url: "https://gdata.youtube.com/feeds/api/users/TheSyndicateProject/playlists?v=2&alt=jsonc",
                dataType:"json",
                success: function(data) {
                    displayPlaylists(data);
                }

            });
    }

the addition of dataType:"json" means that the function expects json data to be returned and therefore parses it on arrival (it is equivalent to using jQuery.parseJSON), however as i said this solution still doesn't work in IE

添加dataType:“json”意味着函数需要返回json数据,因此在到达时解析它(相当于使用jQuery.parseJSON),但正如我所说的这个解决方案仍然无法在IE中工作

4 个解决方案

#1


1  

set dataType to json and browser will automatically parse json for you, see if it works for you

将dataType设置为json,浏览器将自动为您解析json,看看它是否适合您

  function getdata(){
            $.ajax({
                    type:"GET",
                    dataType:'json',
                    contentType:'application/json',
                    url: "https://gdata.youtube.com/feeds/api/users/TheSyndicateProject/playlists?v=2&alt=jsonc",
                    data: "",
                    success: function(data) {                           
                        displayPlaylists(data);
                    }

                });
        }

#2


2  

Since jQuery already assumes it's JSON (from the HTTP header), data already is passed as JSON.

由于jQuery已经假定它是JSON(来自HTTP头),因此数据已经作为JSON传递。

So what happens is that you're using JSON.parse on an object (the JSON).

所以会发生什么是你在一个对象(JSON)上使用JSON.parse。

This fais with the same reason:

这个fais有同样的原因:

JSON.parse({}); // parse an object

because the {} becomes a string:

因为{}变成了一个字符串:

[object Object]

which is not valid JSON at all.

这根本不是有效的JSON。

So, just remove the JSON.parse.

所以,只需删除JSON.parse即可。

#3


1  

My javascript linter indicates that there's a unicode character in the JSON data which it chokes on. This shouldn't be the case as it's valid JSON but this bit:

我的javascript linter表示JSON数据中有一个unicode字符,它会被扼杀。这不应该是这种情况,因为它是有效的JSON,但这一点:

"The Syndicate Challenge #4 World At War\r\nhttp://www.youtube.com/watch?v\u003d49g7f5lkvQ8"

Seems to be the trouble causer. I'm not a 100% sure tho, as firefox seems to be able to parse the data anyway. The unicode character also seems out of place, but that could be me.

似乎是麻烦的麻烦。我不是100%肯定的,因为firefox似乎无论如何都能够解析数据。 unicode角色似乎也不合适,但那可能就是我。

#4


1  

Quite ironically, I have had that same problem in Chrome during the last week, but I suspect that it's not only with Chrome but with any browser.

具有讽刺意味的是,我在上周的Chrome中遇到了同样的问题,但我怀疑它不仅适用于Chrome,而且适用于任何浏览器。

Using Chrome Javascript Debugger, place a breakpoint on the like where you fill the var 'obj' and check if 'data' has more than one object contained inside the array. In case it does, you're probably trying to access the object that is inside the first index position of your data object.

使用Chrome Javascript调试器,在填充var'obj'的位置放置一个断点,并检查“data”是否包含多个对象。如果是这样,您可能正在尝试访问数据对象的第一个索引位置内的对象。

If you sent an anonymous json object from the server to the page, access the first index, instead of data directly.

如果您从服务器向页面发送了匿名json对象,请直接访问第一个索引而不是数据。

Like so:

像这样:

function getdata(){
        $.ajax({
                type:"GET",
                url: "https://gdata.youtube.com/feeds/api/users/TheSyndicateProject/playlists?v=2&alt=jsonc",
                data: "",
                success: function(data) {
                    var obj = data.data.items;
                    displayPlaylists(obj);
                }

            });
    }

EDIT:

编辑:

I debugged your link myself and after testing it further, I found that you don't even need to parse this object, really. Look at my example above, pass data.data.items to your var obj and it will be loaded with all your objects from within the array.

我自己调试了你的链接,经过进一步测试后,我发现你甚至不需要解析这个对象。查看上面的示例,将data.data.items传递给var obj,它将从数组中加载所有对象。

See if that helps.

看看是否有帮助。

#1


1  

set dataType to json and browser will automatically parse json for you, see if it works for you

将dataType设置为json,浏览器将自动为您解析json,看看它是否适合您

  function getdata(){
            $.ajax({
                    type:"GET",
                    dataType:'json',
                    contentType:'application/json',
                    url: "https://gdata.youtube.com/feeds/api/users/TheSyndicateProject/playlists?v=2&alt=jsonc",
                    data: "",
                    success: function(data) {                           
                        displayPlaylists(data);
                    }

                });
        }

#2


2  

Since jQuery already assumes it's JSON (from the HTTP header), data already is passed as JSON.

由于jQuery已经假定它是JSON(来自HTTP头),因此数据已经作为JSON传递。

So what happens is that you're using JSON.parse on an object (the JSON).

所以会发生什么是你在一个对象(JSON)上使用JSON.parse。

This fais with the same reason:

这个fais有同样的原因:

JSON.parse({}); // parse an object

because the {} becomes a string:

因为{}变成了一个字符串:

[object Object]

which is not valid JSON at all.

这根本不是有效的JSON。

So, just remove the JSON.parse.

所以,只需删除JSON.parse即可。

#3


1  

My javascript linter indicates that there's a unicode character in the JSON data which it chokes on. This shouldn't be the case as it's valid JSON but this bit:

我的javascript linter表示JSON数据中有一个unicode字符,它会被扼杀。这不应该是这种情况,因为它是有效的JSON,但这一点:

"The Syndicate Challenge #4 World At War\r\nhttp://www.youtube.com/watch?v\u003d49g7f5lkvQ8"

Seems to be the trouble causer. I'm not a 100% sure tho, as firefox seems to be able to parse the data anyway. The unicode character also seems out of place, but that could be me.

似乎是麻烦的麻烦。我不是100%肯定的,因为firefox似乎无论如何都能够解析数据。 unicode角色似乎也不合适,但那可能就是我。

#4


1  

Quite ironically, I have had that same problem in Chrome during the last week, but I suspect that it's not only with Chrome but with any browser.

具有讽刺意味的是,我在上周的Chrome中遇到了同样的问题,但我怀疑它不仅适用于Chrome,而且适用于任何浏览器。

Using Chrome Javascript Debugger, place a breakpoint on the like where you fill the var 'obj' and check if 'data' has more than one object contained inside the array. In case it does, you're probably trying to access the object that is inside the first index position of your data object.

使用Chrome Javascript调试器,在填充var'obj'的位置放置一个断点,并检查“data”是否包含多个对象。如果是这样,您可能正在尝试访问数据对象的第一个索引位置内的对象。

If you sent an anonymous json object from the server to the page, access the first index, instead of data directly.

如果您从服务器向页面发送了匿名json对象,请直接访问第一个索引而不是数据。

Like so:

像这样:

function getdata(){
        $.ajax({
                type:"GET",
                url: "https://gdata.youtube.com/feeds/api/users/TheSyndicateProject/playlists?v=2&alt=jsonc",
                data: "",
                success: function(data) {
                    var obj = data.data.items;
                    displayPlaylists(obj);
                }

            });
    }

EDIT:

编辑:

I debugged your link myself and after testing it further, I found that you don't even need to parse this object, really. Look at my example above, pass data.data.items to your var obj and it will be loaded with all your objects from within the array.

我自己调试了你的链接,经过进一步测试后,我发现你甚至不需要解析这个对象。查看上面的示例,将data.data.items传递给var obj,它将从数组中加载所有对象。

See if that helps.

看看是否有帮助。