Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a number of clickable objects on the screen that represent objects within a piece of software being interfaced 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 particular command that 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 others, throwing an "Uncaught SyntaxError: Unexpected number" exception.

The JS 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 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?

like image 644
sxthomson Avatar asked Oct 21 '11 13:10

sxthomson


2 Answers

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.

like image 58
Araziah Avatar answered Nov 06 '22 13:11

Araziah


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:

{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:

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

Good luck!

like image 43
rafiki_rafi Avatar answered Nov 06 '22 12:11

rafiki_rafi