Home > Web Front-end > JS Tutorial > Why Does My jQuery Ajax Request Return 200 OK But Trigger the Error Event?

Why Does My jQuery Ajax Request Return 200 OK But Trigger the Error Event?

Linda Hamilton
Release: 2024-11-27 18:11:13
Original
816 people have browsed it

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
});
Copy after login

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\" }");
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template