Ajax request returns 200 OK, but an error event is fired instead of success

后端 未结 16 2111
难免孤独
难免孤独 2020-11-22 04:12

I have implemented an Ajax request on my website, and I am calling the endpoint from a webpage. It always returns 200 OK, but jQuery execut

相关标签:
16条回答
  • 2020-11-22 04:23

    See this. Its also similar problem. Working i tried.

    Dont remove dataType: 'JSON',

    Note: echo only JSON Formate in PHP file if you use only php echo your ajax code return 200

    0 讨论(0)
  • 2020-11-22 04:25

    Use the following code to ensure the response is in JSON format (PHP version)...

    header('Content-Type: application/json');
    echo json_encode($return_vars);
    exit;
    
    0 讨论(0)
  • 2020-11-22 04:29

    I have faced this issue with an updated jQuery library. If the service method is not returning anything it means that the return type is void.

    Then in your Ajax call please mention dataType='text'.

    It will resolve the problem.

    0 讨论(0)
  • 2020-11-22 04:29

    I had the same issue. My problem was my controller was returning a status code instead of JSON. Make sure that your controller returns something like:

    public JsonResult ActionName(){
       // Your code
       return Json(new { });
    }
    
    0 讨论(0)
  • 2020-11-22 04:32

    If you always return JSON from the server (no empty responses), dataType: 'json' should work and contentType is not needed. However make sure the JSON output...

    • is valid (JSONLint)
    • is serialized (JSONMinify)

    jQuery AJAX will throw a 'parseerror' on valid but unserialized JSON!

    0 讨论(0)
  • 2020-11-22 04:32

    Try following

    $.ajax({
        type: 'POST',
        url: 'Jqueryoperation.aspx?Operation=DeleteRow',
        contentType: 'application/json; charset=utf-8',
        data: { "Operation" : "DeleteRow", 
                "TwitterId" : 1 },
        dataType: 'json',
        cache: false,
        success: AjaxSucceeded,
        error: AjaxFailed
    });
    

    OR

    $.ajax({
        type: 'POST',
        url: 'Jqueryoperation.aspx?Operation=DeleteRow&TwitterId=1',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        cache: false,
        success: AjaxSucceeded,
        error: AjaxFailed
    });
    

    Use double quotes instead of single quotes in JSON object. I think this will solve the issue.

    0 讨论(0)
提交回复
热议问题