Javascript jQuery AJAX 函数 - Chrome 抛出“Uncaught SyntaxError: Unexpected number”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7850184/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:49:57  来源:igfitidea点击:

jQuery AJAX function - Chrome throwing "Uncaught SyntaxError: Unexpected number"

javascriptjqueryasp.netjsonp

提问by sxthomson

I have a number of clickable objects on the screen that represent objects within a piece of software being interfaced through a COM component.

我在屏幕上有许多可点击的对象,它们代表通过 COM 组件连接的软件中的对象。

When I click on an object I send the name of the object, the session ID and the command I want to run.

当我单击一个对象时,我会发送该对象的名称、会话 ID 和我想要运行的命令。

The code for the particular command that I'm trying to implement is a C# based ASP.NET page:

我试图实现的特定命令的代码是一个基于 C# 的 ASP.NET 页面:

case "myClick":
                dynamic simObj = S8COM.get_SimObject(Request["id"]);
                responseData = "{name:" + simObj.Name.ToString() + ",countInRoutes:" + simObj.CountInRoutes.ToString() + ",countOutRoutes:" + simObj.CountOutRoutes.ToString() + ",index:" + simObj.Index.ToString() + ",capacity:" + simObj.Capacity.ToString() + ",completed:" + simObj.Completed.ToString() + ",routeOutMethod:" + simObj.RouteOutMethod.ToString() + "}";
                break;

This works fine for some objects, but not others, throwing an "Uncaught SyntaxError: Unexpected number" exception.

这适用于某些对象,但不适用于其他对象,抛出“Uncaught SyntaxError: Unexpected number”异常。

The JS I use to call this particular function is:

我用来调用这个特定函数的 JS 是:

S8Web.Requestmanager.makeRequest({ data: { command: "myClick", id: aItem.id }, async: true, callback: function(data){
                        alert(data.CountInRoutes); //Do a vardump of the response
                        }});

A couple of responses as well, the first one works fine, whereas the second throws the Unexpected Number exception:

还有几个响应,第一个可以正常工作,而第二个会引发意外数量异常:

jsonp1319203225074({name:Start,countInRoutes:0,countOutRoutes:1,index:5,capacity:0,completed:0,routeOutMethod:4});

jsonp1319203225066({name:Process 1,countInRoutes:1,countOutRoutes:1,index:1,capacity:1,completed:0,routeOutMethod:1});

The only thing I can see that could possibly affect the outcome is the whitespace between "Process" and "1". Is that what is throwing this error?

我能看到的唯一可能影响结果的是“Process”和“1”之间的空格。这就是抛出这个错误的原因吗?

回答by Araziah

You very well may simply be having a problem with improperly closed quotes.

您很可能只是遇到了不正确关闭引号的问题。

Example:

例子:

<a href='#' onclick="doStuff('joe, '2844')">click here</a>

Since the first parameter isn't closed properly, it's being interpreted as 'joe, '. That leaves 2844'as the rest of the function call, without a leading quote. This circumstance will throw the Unexpected Number error.

由于第一个参数没有正确关闭,它被解释为'joe, '。这使得2844'作为函数调用的休息,没有一个主导的报价。这种情况将引发意外数字错误。

回答by rafiki_rafi

Not sure if this will help you, but I was getting the same error in chrome and it was because of a "0" that trailed my json data:

不确定这是否对您有帮助,但我在 chrome 中遇到了同样的错误,这是因为我的 json 数据后面有一个“0”:

{id: "6"}0

The 0 trailed the JSON data because I forgot to add an "exit;" in my PHP function that was handling the AJAX call. I also recommend running the same code in FireFox. FireFox a lot of the times has more informative error messages than chrome:

0 落后于 JSON 数据,因为我忘记添加“退出”;在我处理 AJAX 调用的 PHP 函数中。我还建议在 FireFox 中运行相同的代码。很多时候,FireFox 比 chrome 有更多信息丰富的错误消息:

Error: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

错误:SyntaxError:JSON.parse:JSON 数据后出现意外的非空白字符

Good luck!

祝你好运!

回答by thetallone

also had a "0" in my if statement causing unexpected syntax: unexpected number in Chrome, comparison was:

在我的 if 语句中也有一个“0”,导致意外的语法:Chrome 中的意外数字,比较是:

if (bcn.length==0 && ecn.length==0 0 && corr.length==0)

if (bcn.length==0 && ecn.length==0 0 && corr.length==0)

extra 0, see it? I didn't catch it the first few glances.

额外的0,看到了吗?前几眼我没看懂。

回答by Steen

Not sure of the cause of the error, but consider letting a serializer do the work instead of hand coding. Might help take care of different interpretations in browsers.

不确定错误的原因,但可以考虑让序列化程序完成工作而不是手动编码。可能有助于处理浏览器中的不同解释。

In this example, I have a struct with string properties param1 + param2. You could easily serialize lists of these as well.

在这个例子中,我有一个带有字符串属性 param1 + param2 的结构。您也可以轻松地序列化这些列表。

Just construct a simple struct that has the properties you need.

只需构建一个具有所需属性的简单结构即可。

            var jss = new JavaScriptSerializer();
            var jsonApp = new StringBuilder();
            MyStruct item = new MyStruct();
            item.param1 ="111";
            item.param2 ="222";
            jss.Serialize(item, jsonApp); 
            Response.Clear();
            Response.Headers.Add("Content-type", "application/json");
            var resp = HttpContext.Current.Request["callback"] + "(" + jsonApp.ToString() + ")";
            Response.Write(resp);
            Response.End();