Question

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":[]}  
 ]
Was it helpful?

Solution

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
} 

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top