$ .ajax函数的成功处理程序,带有非标准的http状态代码

Ben*_*mma 9 ajax jquery

我们编写了一个RESTful服务器API.无论出于何种原因,我们决定对于DELETE,我们希望返回204(无内容)状态代码,并返回空响应.我试图从jQuery调用它,传入成功处理程序并将动词设置为DELETE:

jQuery.ajax({
    type:'DELETE',
    url: url,
    success: callback,
});
Run Code Online (Sandbox Code Playgroud)

服务器返回204,但永远不会调用成功处理程序.有没有办法配置jQuery以允许204s触发成功处理程序?

Anu*_*rag 11

204应该被视为成功.你使用的是什么版本的jQuery?我做了几个测试,所有200个范围的状态代码都转到了成功处理程序.jQuery 1.4.2的源代码证实了这一点:

// Determines if an XMLHttpRequest was successful or not
httpSuccess: function( xhr ) {
    try {
        // IE error sometimes returns 1223 when 
        // it should be 204 so treat it as success, see #1450
        return !xhr.status && location.protocol === "file:" ||
            // Opera returns 0 when status is 304
            ( xhr.status >= 200 && xhr.status < 300 ) ||
            xhr.status === 304 || xhr.status === 1223 || xhr.status === 0;
    } catch(e) {}

    return false;
},
Run Code Online (Sandbox Code Playgroud)


Pau*_*der 9

我有一个simliar问题因为我的脚本也将"Content-Type"标题发送为"application/json".请求成功后,它无法JSON.parse一个空字符串.