views:

32

answers:

3

hello all, My X-site scripting using JSONP encounters error on IE8 but works fine on both firefox and crome... here the returned JSON object passes an array with a comma at the end and no last element ...i.e something like {a,b,c, } ..as you can see the last element is succeeded by a comma and that is what is causing problem on IE. the actual JSONP response is : gotMsg({"msg":[{"lastMsgId":"69","user":"diptanu","textVal":"Sup","time":"2010-09-04 06:20:15",} ...as you can see the last comma is what it says is causing problem...the error is

Expected identifier, string or number chat.php?callback=gotMsg&user=&lastMsgId=-1&ref=1&userId=-1&loc=http%3A%2F%2Faagmgyd6.yahoo.joyent.us%2Fchat%2Findex.html, line 1 character 96....

Please help thanks Mohan Gupta

A: 

JSON encoders don't usually add a comma, PHP's json_encode doesn't. In JavaScript you shouldn't add a comma after array's last element, but firefox and chrome accept that.

Either try your json-encoder function, or if it's not possible, do some string processing to strip the last comma.

aularon
A: 

IE8 is the only browser to correctly throw an error: This is invalid JSON according to JSONLint.

You would have to get the source fixed somehow, I can't see any way of getting this to work in IE.

Pekka
A: 

To avoid major incompatibility with browser use Aptana Studio or Eclipse/Javascript Editor. They both have possibility to validate the Javascript and JSON objects generated by your server scripts.

Here is the example of correctly constructed JSON object of the personal data. You may get it from WIKI::JavaScript Object Notation

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber": [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
}

When you generate the JSON from an object in PHP you'll better use json_encode method or equivalent in different languages which produces the correctly encoded JSON structure prior to the errors you encountered.

Igor