Understanding jQuery's AJAX Error Response
When sending error responses in jQuery, retrieving the detailed text message can pose a challenge. By default, jQuery only provides a generic 'error' message, leaving you with limited information.
In the example provided, an HTTP 500 error is sent with a custom message "Gone to the beach". However, when using the error callback in jQuery, the console log and alert display only "error".
Accessing the Error Response Text
To access the actual error response text, you can modify the error callback function. Here's how:
<code class="javascript">$.ajax({ type: "post", data: { id: 0 }, cache: false, url: "doIt.php", dataType: "text", error: function(xhr, status, error) { var err = eval("(" + xhr.responseText + ")"); alert("Can't do because: " + err.Message); }, success: function() { alert("Done ! "); } });</code>
In this modified code:
The above is the detailed content of How to Retrieve Detailed Error Messages from jQuery\'s AJAX Error Response?. For more information, please follow other related articles on the PHP Chinese website!