How to create and handle response for Ajax call

JamesB

How is the correct way to handle success/error results in my PHP script? I have a function in the script, and it either fails or succeeds. Normally, I would return true or false.

But if I understand correctly, Ajax success function doesn't care about the result in my script, it only cares whether or not it actually ran my script. In other words, I have to, confusingly, check for errors in the success function.

So how should I have PHP return error, and how do I check for it in the success function? Should I have PHP simply "echo "true";" if it succeeds?

    $.ajax({
        type: 'POST',
        url: 'jquery-actions.php',
        data: formData,
        dataType: 'text',
        encode: true,
        error: function (response) {
            console.log(response);
        },
        success: function (response) {
            //How to find out whether or not PHP function worked?
        }
Rory McCrossan

You are correct in that the error handler of the $.ajax call only executes if the response code from the request is anything other than 200 OK. If you code executes correctly, but you have an error which you want the client side to handle, you would could return a flag indicating the state of the response. Assuming you use JSON, it may look something like this:

{
    success: true,
    additionalData: 'foo bar'
}

Then you can check that flag in your success handler:

$.ajax({
    type: 'POST',
    url: 'jquery-actions.php',
    data: formData,
    dataType: 'text',
    encode: true,
    error: function (response) {
        console.log(response);
    },
    success: function (response) {
        if (response.success) {
            console.log('it worked!');
        } else {
            console.log('there was a problem...');
            console.log(response.additionalData);
        }
    }

Alternatively you could force your PHP to return a 500 error:

header("HTTP/1.1 500 Internal Server Error");

However you would not be able to send additional data with this method, meaning error messages would have to be kept in client side code.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to create an array from AJAX response

分類Dev

How to get data param on the destination page from Ajax response call

分類Dev

how to handle session timeout when ajax call from kendo combobox read action

分類Dev

How to handle 404 as json response if route not found?

分類Dev

How to correctly handle a call function in a function with javascript

分類Dev

PHP: Cannot stream CSV file to client in response to AJAX call

分類Dev

How to handle Ajax browser compatibility without jQuery?

分類Dev

How to display an image that is a response to an API call?

分類Dev

How to obtain response from form submitted by AJAX?

分類Dev

How to use ajax response JSON data?

分類Dev

How to download a file from an AJAX response

分類Dev

how to make the ajax response to prepend div with animation?

分類Dev

Create HTML canvas from Ajax Arraybuffer response type

分類Dev

How to handle a skewed response in H2O algorithms

分類Dev

How to create custom middleware for encrypt webapi response?

分類Dev

How to call Spotify API with Ajax / Jquery?

分類Dev

jQuery - how to access a object displayed by Ajax call?

分類Dev

how to pass a List in the model as a parameter of an ajax call?

分類Dev

AJAX Pagination how to handle new data which is updated in the background(Database)

分類Dev

How to get and print response from Httpclient.SendAsync call

分類Dev

how to get response from rest api callback call

分類Dev

How to create video and voice call to python application?

分類Dev

How to create a function that delays call to given function?

分類Dev

How to filter an html ajax response's elements before inserting into document?

分類Dev

how to return an array from php to ajax response in javascript

分類Dev

How to make button unclickable if Ajax has not received a response

分類Dev

How to collect collect data from Jquery ajax response to html?

分類Dev

How to check on ajax success when the server response is true or false?

分類Dev

How to check on ajax success when the server response is true or false?

Related 関連記事

  1. 1

    How to create an array from AJAX response

  2. 2

    How to get data param on the destination page from Ajax response call

  3. 3

    how to handle session timeout when ajax call from kendo combobox read action

  4. 4

    How to handle 404 as json response if route not found?

  5. 5

    How to correctly handle a call function in a function with javascript

  6. 6

    PHP: Cannot stream CSV file to client in response to AJAX call

  7. 7

    How to handle Ajax browser compatibility without jQuery?

  8. 8

    How to display an image that is a response to an API call?

  9. 9

    How to obtain response from form submitted by AJAX?

  10. 10

    How to use ajax response JSON data?

  11. 11

    How to download a file from an AJAX response

  12. 12

    how to make the ajax response to prepend div with animation?

  13. 13

    Create HTML canvas from Ajax Arraybuffer response type

  14. 14

    How to handle a skewed response in H2O algorithms

  15. 15

    How to create custom middleware for encrypt webapi response?

  16. 16

    How to call Spotify API with Ajax / Jquery?

  17. 17

    jQuery - how to access a object displayed by Ajax call?

  18. 18

    how to pass a List in the model as a parameter of an ajax call?

  19. 19

    AJAX Pagination how to handle new data which is updated in the background(Database)

  20. 20

    How to get and print response from Httpclient.SendAsync call

  21. 21

    how to get response from rest api callback call

  22. 22

    How to create video and voice call to python application?

  23. 23

    How to create a function that delays call to given function?

  24. 24

    How to filter an html ajax response's elements before inserting into document?

  25. 25

    how to return an array from php to ajax response in javascript

  26. 26

    How to make button unclickable if Ajax has not received a response

  27. 27

    How to collect collect data from Jquery ajax response to html?

  28. 28

    How to check on ajax success when the server response is true or false?

  29. 29

    How to check on ajax success when the server response is true or false?

ホットタグ

アーカイブ