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?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
  • do you have any script errors ? check firebug console. – Shyju May 02 '12 at 21:53
  • If you put an alert at the beginning of `sendMessage()`, does it show? – jfriend00 May 02 '12 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. – Anthony Grist May 02 '12 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? – jimw May 02 '12 at 21:56
  • good idea! Strangely I cannot open test.php in my browser. – Dennis Hackethal May 02 '12 at 21:58
  • 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" – Dennis Hackethal May 02 '12 at 21:58
  • 1
    Your url is wrong. Try changing it to /action/chat/test.php – Mike Robinson May 02 '12 at 21:59
  • no the url is definitely correct... – Dennis Hackethal May 02 '12 at 22:00
  • 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? – DanRedux May 02 '12 at 22:04
  • 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? – Mike Robinson May 02 '12 at 22:05
  • 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! – Dennis Hackethal May 02 '12 at 22:07

3 Answers3

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.

Fostah
  • 2,947
  • 4
  • 56
  • 78
  • 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! – Dennis Hackethal May 02 '12 at 22:08
  • 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. – Fostah May 02 '12 at 22:11
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.

Nadir
  • 695
  • 8
  • 12
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.

DanRedux
  • 9,119
  • 6
  • 23
  • 41
  • 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. – Dennis Hackethal May 02 '12 at 22:01
  • 1
    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! – Dennis Hackethal May 02 '12 at 22:08