JQuery AJAX Function - Chrome Metadata "Uncaught SyntaxError: Unexpected Number"

On my screen there are several clickable objects that represent objects inside a piece of software interacting through a COM component.

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

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

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 for others, throwing an "Uncaught SyntaxError: Unexpected number" exception.

The JS that I use to call this particular function is:

 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 answers also, the first works fine, while the second throws an "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 see that can affect the result is the gap between the β€œProcess” and β€œ1”. Is this what throws this error?

+7
source share
4 answers

You may very well have a problem with incorrectly closed quotes.

Example:

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

Since the first parameter is not properly closed, it is interpreted as "joe". This leaves 2844 'like the rest of the function call, with no main quote. This will cause the "Unexpected number" error.

+13
source

Not sure if this will help you, but I was getting the same error in chrome and that was due to the β€œ0” that hooked my json data:

 {id: "6"}0 

0 loaded JSON data because I forgot to add "exit"; in my PHP function that handled the AJAX call. I also recommend using the same code in FireFox. FireFox has more informative error messages than chrome many times:

Error: SyntaxError: JSON.parse: unexpected character with no spaces after JSON data

Good luck

+5
source

Not sure about the cause of the error, but consider having the serializer do the work instead of manual coding. May help take care of different interpretations in browsers.

In this example, I have a structure with string properties param1 + param2. You can also easily serialize their lists.

Just create a simple structure that has the necessary properties.

  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(); 
0
source

also had a β€œ0” in my if statement causing an unexpected syntax: unexpected number in Chrome, the comparison was:

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

extra 0, see it? I did not notice the first glances.

0
source

All Articles