1

I have the following ajax call in a function, which is called upon a clickevent.

$.ajax({
  url:"server.php",
  type:'GET',
  data: {'action':'addEvent'},
  success: function(response)
  {
    if(response)
    {
    alert("200 ok");                
    }
 },
 error: function(xhr, ajaxOptions, ThrownError)
 {
   alert("Error:" + ThrownError);
   $("#output").text("Error: "+ThrownError);
 }
}); 

The php side is as follows.

  if(isset($_GET["action"]))
   {
      $action = $_GET["action"];
     if($action == 'addEvent')
     {
          echo("ping");
     }

   }

Now it throws an error. But the error message i get is :

Exactly, i get Error:

It is driving me crazy.

Also i put in my document.ready, to let it act as some sort of ping, and it worked perfectly

6
  • @Dev that was exactly my problem, if i had an error this question would not be here Commented Oct 30, 2012 at 12:26
  • 1
    What is exact response you receive? You can take a look at it in developer tool (F12, you may need to install FireBug in FF), network tab or something similar Commented Oct 30, 2012 at 12:34
  • I get no response, absolutely nothing. But it does give a response when i put in document.ready() Commented Oct 30, 2012 at 12:36
  • Click event on what? Link, form button, something else? Commented Oct 30, 2012 at 12:40
  • Can you post whole click event handler? Commented Oct 30, 2012 at 12:40

2 Answers 2

2

Take a look at this jsFiddle. I'm getting there same error you have.

Why that is happening there: link is clicked and ajax request is sent. But default action is not stopped and browser navigates to url specified in href and stops any JS execution. AJAX call is stopped too.

In order to prevent that, you should do something like this:

$('a').click(function(e) { 
    $.ajax({
      url:"server.php",
      type:'GET',
      data: {'action':'addEvent'},
      success: function(response) {
        if(response) {
           alert("200 ok");                
        }
      },
      error: function(xhr, ajaxOptions, ThrownError) {
         alert("Error:" + ThrownError);
         $("#output").text("Error: "+ThrownError);
      }
   }); 
   e.preventDefault();//this will not allow browser to move to a different URL
   return false; //alternative to e.preventDefault(). 
});​

Possibly you have similar problem, but it is hard to notice because your element is causing simple page reload, browser navigates to the same page.

Sign up to request clarification or add additional context in comments.

1 Comment

This helped. I just got success. This just worked. O my god. So epic. Thank you.
0

The function definition for error is the following:

error(jqXHR, textStatus, errorThrown)

further, try to get more info by defining the following

statusCode

A map of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:

$.ajax({
  statusCode: {
    404: function() {
      alert('page not found');
    }
  }
});

If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback.

success(data, textStatus, jqXHR)

so now, you have two more functions defined(success + http code). so if the request fails, it would execute code in error, if it successful it will execute success. In both case, you will get the http response code. check if the code is 200, then everything went well. 404 - the script url is wrong. 500 - there is a server error & so on.

1 Comment

when i print jqXHR.status i get 0. But i am not sure that this is the actual statusCode, is there a way i can output the statusCode?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.