9

I am having a problem with a simple callback function in jQuery ajax. Google won't help and stack overflow wouldn't either, so I guess this might not be something specific but rather something I am too ignorant to see. To me the code looks exactly like it should.

So, here's the code:

function sendMessage(message)
{
//Establish connection to php script
$.ajax({
    type: 'POST',
    url: 'action/chat/test.php',
    success: function(feedback){

        alert(feedback);

    }
}).error(function(){
    //Do some error handling here
});
}

In test.php it simply says

<?php
    echo "called";
?>

As far as I am concerned "called" should be alerted - but it isn't. I have checked already that the function sendMessage() is called (and the parameter message doesn't matter for now).

Does anyone have any idea?

11
  • do you have any script errors ? check firebug console. Commented May 2, 2012 at 21:53
  • If you put an alert at the beginning of sendMessage(), does it show? Commented May 2, 2012 at 21:55
  • The two likeliest options: Javascript errors causing the AJAX request not to be sent, or a problem with the AJAX request that means the error, rather than success, callback function is executed. Using your browsers debugger would be a good first step. Commented May 2, 2012 at 21:55
  • Does it work if you visit action/chat/test.php in your browser? Do you server logs show that file being hit when you run the AJAX? Commented May 2, 2012 at 21:56
  • 1
    Your url is wrong. Try changing it to /action/chat/test.php Commented May 2, 2012 at 21:59

3 Answers 3

13

Update: One thing to note also, make sure you use some kind of debugger like firebug. Then you can go to the network tab and look at the request url and response manually to see if its getting a 200 response or internal server error, etc.

Try adding a console.log(data); in your success function to see if anything is being returned.

You could also use .always(data):

function sendMessage(message)
{
  //Establish connection to php script
  $.ajax({
      type: 'POST',
      url: 'action/chat/test.php'   
  }).done(function(data) { console.log(data); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });
}

From the docs:

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

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

2 Comments

I have found the problem! There was some issue with file access from my apache. Apache was not allowed to read the file - although I just created it with Coda as I usually do. Now the callback works. I just joined stack overflow and have to say that I am overwhelmed with all the quick answers! Thank you!
One thing to note also, make sure you use some kind of debugger like firebug. Then you can go to the network tab and look at the request url and response manually to see if its getting a 200 response or internal server error, etc.
4

Just for the reference, there is a behavior that may end up like this (ie done() not called).

Here it is:

  1. Suppose you expect a JSON object (you asked for it with the "Accept" mime type).
  2. Suppose the Json string is not valid.

In this case done() is never called, but always() will be. And in always() you will get the "badly" formatted answer as pure text.

Comments

1

It's probably the error handling. If you want to handle errors, there's another attribute you can give the object you pass in to ajax, it's "error", and also "timeout" for handling page timeouts. Look up the $.ajax function and you'll find these attributes.

2 Comments

When I tried to use the error handling before with an alert, it didn't alert anything. Now it does. There seems to be something wrong with the php file, although the path is correct.
I have found the problem! There was some issue with file access from my apache. Apache was not allowed to read the file - although I just created it with Coda as I usually do. Now the callback works. I just joined stack overflow and have to say that I am overwhelmed with all the quick answers! Thank you!

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.