Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery ajax error function is executed even if query is successful

Tags:

jquery

ajax

xml

I'm trying to learn JQuery - and I have a small problem with ajax. I'm trying to populate a javascript array with values returned from an XML response from a page.
Here's my main page (ajax.html):

<html>
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript" src="jquery/fiber.js"></script>
</head>
<body>
<p>Ajax</p>
<script>
var ringType = new Array();

</script>
</body>
</html>

fiber.js is this:

//process things for fiber map
jQuery(document).ready(function() {
// do stuff when DOM is ready

//populate and display ringType
$.ajax({
  type: "GET",
  url: "ajaxHelper.pl",
  data: {
      getRingTypes: "1",
      },
  dataType: "xml",
  success: function(xml) {
      //if the query was successfull,
      alert("Got an xml object:"+$(xml));
      $(xml).find("ringType").each( function(){
          alert("Received reply "+$(this).text());
          var type = $(this).html(); //save the value
          //append to ringType array
          ringType.push(type);
      });
  },
  error:function (xhr, ajaxOptions, thrownError){
      alert(xhr.status);
      alert(thrownError);
  }
 });
 for(var i=0; i<ringType.length; i++){
    document.write("<br>"+ringType[i]);
 }

});

ajaxHelper.pl generates this XML (without the backslashes in \?) (as content-type text/xml):

<?xml version="1.0" encoding="ISO-8859-1"?>
    <\?xml version="1.0" encoding="ISO-8859-1"\?>
    <ringType>IA</ringType>
    <ringType>IL</ringType>
    <ringType>IN</ringType>
    <ringType>IR</ringType>
    <ringType>RT</ringType>

The problem is, every time I load ajax.html, the ajax query is successful, but the error function is executed! xhr.status = 200 (meaning the query was ok) and thrownException is undefined.

like image 599
Adrian Avatar asked Jun 09 '09 09:06

Adrian


People also ask

What triggers Ajax error?

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the . ajaxError() method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

What is Ajax error function?

The ajaxError() method is used to attach a function to be run when an AJAX request fails. It is an AJAX event. jQuery triggers the ajaxError event when an AJAX request completes with an error.

What is success and error function in Ajax?

success and Error : A success callback that gets invoked upon successful completion of an Ajax request. A failure callback that gets invoked in case there is any error while making the request.

How can Ajax call error be resolved?

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.


1 Answers

via http://groups.google.com/group/jquery-en/browse_thread/thread/23679594ebe128a9

the server could return an XML document with a 200 status code. But if the browser can not parse the document a parseerror will occur and jQuery's error handler will be invoked.

Make sure you're returning valid xml :)

like image 128
bloudermilk Avatar answered Oct 15 '22 14:10

bloudermilk