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

后端 未结 16 1998
难免孤独
难免孤独 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:14

    I reckon your aspx page doesn't return a JSON object. Your page should do something like this (page_load)

    var jSon = new JavaScriptSerializer();
    var OutPut = jSon.Serialize(<your object>);
    
    Response.Write(OutPut);
    

    Also, try to change your AjaxFailed:

    function AjaxFailed (XMLHttpRequest, textStatus) {
    
    }
    

    textStatus should give you the type of error you're getting.

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

    I have the similar problem but when I tried to remove the datatype:'json' I still have the problem. My error is executing instead of the Success

    function cmd(){
        var data = JSON.stringify(display1());
        $.ajax({
            type: 'POST',
            url: '/cmd',
            contentType:'application/json; charset=utf-8',
            //dataType:"json",
            data: data,
            success: function(res){
                      console.log('Success in running run_id ajax')
                      //$.ajax({
                       //   type: "GET",
                       //   url: "/runid",
                       //   contentType:"application/json; charset=utf-8",
                       //   dataType:"json",
                       //   data: data,
                       //  success:function display_runid(){}
                      // });
            },
            error: function(req, err){ console.log('my message: ' + err); }
        });
    }
    
    0 讨论(0)
  • 2020-11-22 04:17

    I've had some good luck with using multiple, space-separated dataTypes (jQuery 1.5+). As in:

    $.ajax({
        type: 'POST',
        url: 'Jqueryoperation.aspx?Operation=DeleteRow',
        contentType: 'application/json; charset=utf-8',
        data: json,
        dataType: 'text json',
        cache: false,
        success: AjaxSucceeded,
        error: AjaxFailed
    });
    
    0 讨论(0)
  • 2020-11-22 04:17

    You simply have to remove the dataType: "json" in your AJAX call

    $.ajax({
        type: 'POST',
        url: 'Jqueryoperation.aspx?Operation=DeleteRow',
        contentType: 'application/json; charset=utf-8',
        data: json,
        dataType: 'json', //**** REMOVE THIS LINE ****//
        cache: false,
        success: AjaxSucceeded,
        error: AjaxFailed
    });
    
    0 讨论(0)
  • 2020-11-22 04:21

    jQuery.ajax attempts to convert the response body depending on the specified dataType parameter or the Content-Type header sent by the server. If the conversion fails (e.g. if the JSON/XML is invalid), the error callback is fired.


    Your AJAX code contains:

    dataType: "json"
    

    In this case jQuery:

    Evaluates the response as JSON and returns a JavaScript object. […] The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. […] an empty response is also rejected; the server should return a response of null or {} instead.

    Your server-side code returns HTML snippet with 200 OK status. jQuery was expecting valid JSON and therefore fires the error callback complaining about parseerror.

    The solution is to remove the dataType parameter from your jQuery code and make the server-side code return:

    Content-Type: application/javascript
    
    alert("Record Deleted");
    

    But I would rather suggest returning a JSON response and display the message inside the success callback:

    Content-Type: application/json
    
    {"message": "Record deleted"}
    
    0 讨论(0)
  • 2020-11-22 04:22

    You just have to remove dataType: 'json' from your header if your implemented Web service method is void.

    In this case, the Ajax call don't expect to have a JSON return datatype.

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