How to handle ajax 201

后端 未结 5 1065
说谎
说谎 2020-12-31 09:16

When making a ajax call see example below success does gets a 201 status retuned. How do you handle these better i.e. 200, 201 within the succe

5条回答
  •  再見小時候
    2020-12-31 10:03

    Use the statusCode object:

    var handle200 = function(data, textStatus, jqXHR) {
        alert('200'); // success codes have the success signature
    };
    
    var handle201 = function(data, textStatus, jqXHR) {
        alert('201'); // success codes have the success signature
        // test it if you are in doubt:
        console.log(data);
        console.log(textStatus);
        console.log(jqXHR);
    };
    
    var handle404 = function(jqXHR, textStatus, errorThrown) {
        alert('404'); // failing codes have the error signature
    });
    
    var request = $.ajax({
        type: 'POST',
        url: '/myresource/posttarget',
        data: { name: 'john' },
        statusCode: {
            200: handle200,
            201: handle201,
            404: handle404
        }
    });
    

提交回复
热议问题