0

A form is being submitted using the ajax code below. I am unsure what response to generate using PHP, so that $.ajax can call the appropriate callbacks done() and fail()

request = $.ajax({
        url: "php_process.php",
        type: "post",
        data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");

    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });`
dr_rk
  • 4,395
  • 13
  • 48
  • 74
  • `.fail` will be called when your script responds with an HTTP error code, `.done` otherwise. – CBroe Aug 13 '14 at 23:59
  • The failure is called based upon a request fail, so there should be no concern about what the PHP does. The PHP never succeeds in running at all. – XaxD Aug 14 '14 at 00:00
  • send something back that you can validate in the `done` handler, can be text, json or whatever data format you prefer – charlietfl Aug 14 '14 at 00:02
  • How can I return a json back from php so that i can do `response.some_key` – dr_rk Aug 14 '14 at 15:32
  • @dr_rk Updated jsfiddle link at post http://jsfiddle.net/guest271314/L3jbvnex/1/ – guest271314 Aug 20 '14 at 19:34

2 Answers2

0

See .done() and .fail() responses at result area , network tab at console

$(function() {
    var urls = ["/echo/jsons/", "/echo/json/"];
    var request = function(url) {

        return $.ajax({
        url: url,
        type: "POST",
        data: {json : JSON.stringify({"abc":[123]}) }            
        });
    };           
    // callback handler that will be called on success
 $.each(urls, function(k, v) {
     $.when(request(v))
    .done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!", response);
        $("body").prepend("DONE: <br>" 
                          + Object.keys(response) + ":" 
                          +  response[Object.keys(response)] 
                          + "<br><br>")
    })    
    // callback handler that will be called on failure
   .fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.log("The following error occured: "
                    + textStatus, errorThrown);
       $("textarea")
       .before("FAIL: <br>")
       .val(jqXHR.getAllResponseHeaders() +"\n" 
           + jqXHR.status +"\n"+ textStatus 
           +"\n"+ errorThrown +"\n" +  jqXHR.responseText)
    });  
});
});

jsfiddle http://jsfiddle.net/guest271314/L3jbvnex/1/

See

PHP: How to send HTTP response code?

http://php.net/manual/en/function.http-response-code.php

http://php.net/manual/en/function.header.php

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
-1

fail() is only called if there is an error in getting the response from server. Else the done() is called. So fail() doesn't depend on the response from the PHP.

webNeat
  • 2,768
  • 1
  • 20
  • 22