Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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?
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!
–
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.
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();
also had a "0" in my if statement causing unexpected syntax: unexpected number in Chrome, comparison was:
if (bcn.length==0 && ecn.length==0 0 && corr.length==0)
extra 0, see it? I didn't catch it the first few glances.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.