未捕获的语法错误意外的令牌 U JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13022178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
uncaught syntaxerror unexpected token U JSON
提问by kinath_ru
I get this error "uncaught syntaxerror unexpected token U" when I run my page in chrome. And in firefox I get, "JSON.parse: unexpected character". I'm returning the json data from a php file and the returning json string is valid. I checked it with http://jsonlint.com/. Any help would be appreciated... Thanks.
当我在 chrome 中运行我的页面时,我收到此错误“未捕获的语法错误意外标记 U”。在 Firefox 中,我得到了“JSON.parse:意外字符”。我正在从 php 文件返回 json 数据,并且返回的 json 字符串是有效的。我用http://jsonlint.com/检查了它。任何帮助将不胜感激...谢谢。
Here's the returned JSON string
这是返回的 JSON 字符串
[
["1","Pan Africa Market","\"1521 1st Ave, Seattle, WA\"","47.608941","-122.340145","restaurant"],
["2","The Melting Pot","14 Mercer St, Seattle, WA","47.624562","-122.356442","restaurant"],
["3","Ipanema Grill","1225 1st Ave, Seattle, WA","47.606366","-122.337656","restaurant"],
["4","Sake House","230 1st Ave, Seattle, WA","47.612825","-122.34567","bar"],
["5","Crab Pot","1301 Alaskan Way, Seattle, WA","47.605961","-122.34036","restaurant"],
["6","Mexican Kitchen","2234 2nd Ave, Seattle,WA","47.613975","-122.345467","bar"],
["7","Wingdome","1416 E Olive Way, Seattle, WA","47.617215","-122.326584","bar"],
["8","Piroshky Piroshky","1908 Pike pl, Seattle, WA","47.610127","-122.342838","restaurant"]
]
回答by Sean Kinsey
That error is normally seen when the value given to JSON.parseis actually undefined.
So, I would check the code that is trying to parse this - most likely you are not parsing the actual string shown here.
当给定的值JSON.parse实际上是 时,通常会看到该错误undefined。所以,我会检查试图解析这个的代码 - 很可能你没有解析这里显示的实际字符串。
回答by user1412699
I was getting this message while validating (in MVC project). For me, adding ValidationMessageForelement fixed the issue.
我在验证时收到此消息(在 MVC 项目中)。对我来说,添加ValidationMessageFor元素解决了这个问题。
To be precise, line number 43 in jquery.validate.unobtrusive.js caused the issue:
准确地说,jquery.validate.unobtrusive.js 中的第 43 行导致了这个问题:
replace = $.parseJSON(container.attr("data-valmsg-replace")) !== false;
回答by Kartik ganiga
The parameter for the JSON.parse may be returning nothing (i.e. the value given for the JSON.parse is undefined)!
JSON.parse 的参数可能什么都不返回(即 JSON.parse 的值是undefined)!
It happened to me while I was parsing the Compiled solidity code from an xyz.sol file.
当我从 xyz.sol 文件解析 Compiled solidity 代码时,它发生在我身上。
import web3 from './web3';
import xyz from './build/xyz.json';
const i = new web3.eth.Contract(
JSON.parse(xyz.interface),
'0x99Fd6eFd4257645a34093E657f69150FEFf7CdF5'
);
export default i;
which was misspelled as
被拼错为
JSON.parse(xyz.intereface)
which was returning nothing!
什么都没有返回!
回答by Matas Vaitkevicius
Most common case of this error happening is using template that is generating the control then changing the way idand/or nameare being generated by 'overriding' default template with something like
发生此错误的最常见情况是使用生成控件的模板,然后更改方式id和/或name通过“覆盖”默认模板生成,例如
@Html.TextBoxFor(m => m, new {Name = ViewData["Name"], id = ViewData["UniqueId"]} )
and then forgetting to change ValidationMessageForto
然后忘了更改ValidationMessageFor,以
@Html.ValidationMessageFor(m => m, null, new { data_valmsg_for = ViewData["Name"] })
Hope this saves you some time.
希望这可以为您节省一些时间。
回答by SD1985
I experienced this error when I ran a find condition on a JSONArray within a for loop. The problem I faced was the result of one of the values in the for loop returning null. Hence, when I tried to access a property on that it failed.
当我在 for 循环中对 JSONArray 运行查找条件时遇到此错误。我面临的问题是 for 循环中的一个值返回 null 的结果。因此,当我尝试访问它失败的属性时。
So if you are doing anything within JSONArrays where you are not sure of the data source and its integrity I thinks its a good habit to deal with null and undefined exceptions in this case.
因此,如果您在 JSONArrays 中执行任何不确定数据源及其完整性的操作,我认为在这种情况下处理 null 和未定义异常是一个好习惯。
I fixed it by checking for null on the returned value of find on the JSONArray and handling exceptions appropriately.
我通过检查 JSONArray 上 find 的返回值是否为 null 并适当处理异常来修复它。
Thought this might help.
认为这可能会有所帮助。
回答by delliottg
In my case it was trying to call JSON.parse() on an AJAX variable before the XHRResponse came back. EG:
就我而言,它试图在 XHRResponse 返回之前对 AJAX 变量调用 JSON.parse()。例如:
var response = $.get(URL that returns a valid JSON string);
var data = JSON.parse(response.responseText);
I replaced that with an example out of the jQuery site for $.get:
我用$.get的jQuery 站点中的示例替换了它:
<script type="text/javascript">
var jqxhr = $.get( "https://jira.atlassian.com/rest/api/2/project", function() {
alert( "success" );
})
.done(function() {
//insert code to assign the projects from Jira to a div.
jqxhr = jqxhr.responseJSON;
console.log(jqxhr);
var div = document.getElementById("products");
for (i = 0; i < jqxhr.length; i++) {
console.log(jqxhr[i].name);
div.innerHTML += "<b>Product: " + jqxhr[i].name + "</b><BR/>Key: " + jqxhr[i].key + "<BR/>";
}
console.log(div);
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});
</script>
回答by Goodlife
Just incase u didnt understand
以防万一你不明白
e.g is that lets say i have a JSON STRING ..NOT YET A JSON OBJECT OR ARRAY.
例如,可以说我有一个 JSON 字符串 .. 还没有一个 JSON 对象或数组。
so if in javascript u parse the string as
所以如果在javascript中你将字符串解析为
var body={
"id": 1,
"deleted_at": null,
"open_order": {
"id": 16,
"status": "open"}
var jsonBody = JSON.parse(body.open_order); //HERE THE ERROR NOW APPEARS BECAUSE THE STRING IS NOT A JSON OBJECT YET!!!!
//TODO SO
var jsonBody=JSON.parse(body)//PASS THE BODY FIRST THEN LATER USE THE jsonBody to get the open_order
var OpenOrder=jsonBody.open_order;
Great answers above
上面的好答案
回答by Kushal Parikh
This answer if for the people who are facing this error while working with File Upload..
对于在使用File Upload时遇到此错误的人来说,此答案是...
We were using middleware for token based encryption - decryption and we encountered same error.
我们使用中间件进行基于令牌的加密 - 解密,我们遇到了同样的错误。
Following was our code in route file:
以下是我们在路由文件中的代码:
router.route("/uploadVideoMessage")
.post(
middleware.checkToken,
upload.single("video_file"),
videoMessageController.uploadVideoMessage
);
here we were calling middleware before upload function and that was causing the error. So when we changed it to this, it worked.
在这里,我们在上传功能之前调用了中间件,这导致了错误。所以当我们把它改成这个时,它起作用了。
router.route("/uploadVideoMessage")
.post(
upload.single("video_file"),
middleware.checkToken,
videoMessageController.uploadVideoMessage
);
回答by mk Mughal
This is not a difficult task. That problem also occur at my site you should have to shift your js files top ordered. Because at the place where you are using JSON Parsing, this time your JS files are not loaded. EXAMPLE #
这不是一项艰巨的任务。这个问题也发生在我的网站上,你应该把你的 js 文件按顺序排列。因为在你使用 JSON Parsing 的地方,这一次你的 JS 文件并没有加载。例子 #
<script type="text/javaScript">
...........SOME CODE.............
</script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
change to
改成
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script type="text/javaScript">
...........SOME CODE.............
</script>
回答by Nancy256
Well, to all who actually can't find the error anywhere in your code, neither "undefined as it says in local storage nor null"..simply just comment out your code and write another one that actually removes the item in local storage ..after that ,u can comment or delet the current code and reset again the previous one by simply uncommenting it out (if u dint delet t ...if u did u can write it again:))
好吧,对于所有实际上在您的代码中的任何地方都找不到错误的人,既不是“未定义,如本地存储中所说的,也不是空的”……只需注释掉您的代码并编写另一个实际删除本地存储中的项目的代码。 .之后,您可以评论或删除当前代码并通过简单地取消注释来重新设置前一个代码(如果您不想删除...如果您这样做了,您可以再次编写它:))
LocalStorage.setItem('Previous' , "removeprevious");
LocalStorage.removeItem('Previous');
Console.log(LocalStorage.getItem('Previous'));
Console will show null and that it ..reset your code again if t doesn't work, dude!you got errors.
控制台将显示 null 并且它 .. 如果 t 不起作用,它会再次重置您的代码,伙计!你有错误。
sorry for my English!
对不起我的英语不好!

