Why does jquery ajax callback function not work?
Asked Answered
E

3

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?

Erase answered 2/5, 2012 at 21:51 Comment(11)
do you have any script errors ? check firebug console.Jocelyn
If you put an alert at the beginning of sendMessage(), does it show?Prune
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.Luteous
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?Barreto
good idea! Strangely I cannot open test.php in my browser.Erase
it says "Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. If you think this is a server error, please contact the webmaster. Error 404"Erase
Your url is wrong. Try changing it to /action/chat/test.phpCrush
no the url is definitely correct...Erase
Then we definitely need more information. You need to find out why the page isn't responding. Find the real location of it. Do you have any frameworks?Rycca
The url is 404'ing. It may be structurally correct, but your script can't find it. Can you type it into your browser and get a result?Crush
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!Erase
S
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.

Styria answered 2/5, 2012 at 22:1 Comment(2)
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!Erase
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.Styria
B
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.

Bergmans answered 29/7, 2014 at 9:50 Comment(0)
R
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.

Rycca answered 2/5, 2012 at 21:55 Comment(2)
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.Erase
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!Erase

© 2022 - 2024 — McMap. All rights reserved.