Home >Web Front-end >JS Tutorial >Why Does My jQuery Ajax Request Return 200 OK But Trigger the Error Event?
jQuery Ajax Request Returns Success (200 OK) but Triggers Error Event
Your jQuery Ajax request is always returning 200 OK, indicating a successful response. However, jQuery is still executing the error event instead of the success event.
The Issue:
When using jQuery's $.ajax, if the dataType parameter is set to "json" (as in your code), it expects the response from the server to be valid JSON. However, in your case, the server response is HTML, which is not valid JSON. This causes jQuery to throw a parse error and trigger the error event.
Solution 1: Remove dataType: 'json'
One solution is to remove the dataType parameter from your Ajax request. This will allow jQuery to interpret the response as plaintext, which will include the HTML you're sending from your server.
$.ajax({ type: 'POST', url: 'Jqueryoperation.aspx?Operation=DeleteRow', contentType: 'application/json; charset=utf-8', data: json, cache: false, success: AjaxSucceeded, error: AjaxFailed });
Solution 2: Return JSON from Server
Alternatively, you can modify your server-side C# code to return JSON instead of HTML. This will ensure that jQuery can parse the response correctly and execute the success event.
private void test() { Response.ContentType = "application/json"; Response.Write("{ \"message\": \"Record Deleted\" }"); }
With this modification, jQuery will receive a valid JSON response and trigger the success event as expected.
The above is the detailed content of Why Does My jQuery Ajax Request Return 200 OK But Trigger the Error Event?. For more information, please follow other related articles on the PHP Chinese website!