1

i am currently working on a program in jquery. My program works fine in firefox 3.5 but not until i upgraded my browser to firefox 4.0. Since then, the 'parsererror' never failed to show and it gives me a bad headache.

I've notice that this is the part of my code that FIRST 'parsererror' shows:

$(document).ready( function() { 
   ...

   $.ajaxSetup({
      timeout: 10000,
      error: function(xhr, msg, e) {
        showMessage('HTTP error: ' + JSON.stringify(msg) + '.'); //this is the parsererror
      }    
   });  
   .
   .
})  

And not only that, my dynamic tab no longer appear in my page. I notice that everytime if remove this line '<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>', it appears but my other jquery element is no good. I don't know what's wrong. Maybe it some sort of incompatibility issues, but i just dont where to start fixing. Please help.

EDIT: this is the json it returned. This is supposedly for my dynamic menu that will create tab Evry menu you clicked. But this doesn't show.

 [ 
      {"title": "File","submenus":[
         {"title": "Open","submenus":[]},         
         { "title": "New", "submenus":[]},
         { "title": "Save as", "submenus":[]},
         { "title": "Save", "submenus":[]}
      ]},
      { "title": "View","submenus":[]},
      { "title": "viewAll", "submenus":[]},
      { "title": "Close","submenus":[]},
      {"title":"jQgrid", "submenus":[]}  
 ]
jayAnn
  • 827
  • 3
  • 18
  • 38
  • Can you provide a sample of what the JSON data looks like? – onteria_ May 12 '11 at 02:57
  • My JSON is from server side. I just convert it to json so that the program would be able to read it. here's my sample code : http://stackoverflow.com/questions/5309114/why-is-it-that-i-cannot-sort-my-grid-in-jqgrid-everytime-i-clicked-the-column-hea – jayAnn May 12 '11 at 03:07

2 Answers2

2

Description from jQuery Ajax's error documentation

error(jqXHR, textStatus, errorThrown)

Function

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event

In your code you have

JSON.stringify(msg)

Looking at the jQuery docs, you will see that the second argument is a string and not a JSON object like you are expecting it to be. The parser is seeing the string and throws the parse error that you are seeing.

Now if an error is occurring and the object is saying that there is an error. JSON requires that the name has double quotes around it. So people think:

{
  foo : "bar",
  color : "red",
  num : 1
} 

is valid JSON, but it is not. The following is valid.

{
  "foo" : "bar",
  "color" : "red",
  "num" : 1
} 
Community
  • 1
  • 1
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • thank for the answer, but i still can't get it. if i put 'JSON.stringify(msg)', it will display 'HTTP error: "parsererror". If i remove the JSON.stringify, it will also display the same message but only without double quotation mark, 'HTTP error: parsererror.' – jayAnn May 12 '11 at 03:19
  • The JSON that is being returned from the server is invalid. – epascarello May 12 '11 at 03:25
0

It sounds like the code above works perfectly - it's telling you that the XMLHttpRequest returned an error with the message "parsererror". You could probably learn more about it by inspecting the e variable in your error function. But the code you've provided is not causing the error, so there's no way we could debug it.

nrabinowitz
  • 55,314
  • 10
  • 149
  • 165
  • well, as what I have observed in firefox 3.5, there's no problem at all. I can add, delete, search, filter the data from server. Its only in firefox 4.0. that i met this error. How can it be...? and what about my dynamic tab? Hmmmm.... – jayAnn May 12 '11 at 04:13
  • The browser is parsing the data returned from the server differently - maybe it's a stricter JSON or XML parser in the later version of FF. But the code you included in your question doesn't affect this one way or the other - the problem is the data from the server. So we can't debug it without seeing that data. And it would be pretty hard to guess the problem with the tab without seeing any code. – nrabinowitz May 12 '11 at 04:59
  • AKA: Show the Ajax response that is being returned! – epascarello May 12 '11 at 05:14