We've written a RESTful server API. For whatever reason, we made the decision that for DELETEs, we would like to return a 204 (No Content) status code, with an empty response. I'm trying to call this from jQuery, passing in a success handler and setting the verb to DELETE:
我们编写了一个RESTful服务器API。无论出于何种原因,我们决定对于DELETE,我们希望返回204(无内容)状态代码,其响应为空。我试图从jQuery调用它,传入成功处理程序并将动词设置为DELETE:
jQuery.ajax({
type:'DELETE',
url: url,
success: callback,
});
The server returns a 204, but the success handler is never called. Is there a way I can configure jQuery to allow 204s to fire the success handler?
服务器返回204,但永远不会调用成功处理程序。有没有办法配置jQuery以允许204s触发成功处理程序?
5 个解决方案
#1
11
204
should be treated as success. What version of jQuery are you using? I did a couple of tests and all 200 range status codes went to the success handler. The source for jQuery 1.4.2 confirms this:
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;
},
#2
9
I had a simliar issue because my script was also sending the "Content-Type" header as "application/json". While the request succeeded, it couldn't JSON.parse an empty string.
我有一个simliar问题,因为我的脚本也将“Content-Type”标题发送为“application / json”。当请求成功时,它无法JSON.parse一个空字符串。
#3
3
jQuery.ajax({
...
error: function(xhr, errorText) {
if(xhr.status==204) successCallback(null, errorText, xhr);
...
},
...
});
ugly...but might help
丑陋......但可能有所帮助
#4
3
It's technically a problem on your server
Like Paul said, an an empty 204 response with a server content-type of json is treated by jQuery as an error.
You can get around it in jQuery by manually overriding dataType to 'text'.
从技术上讲,它在你的服务器上是一个问题就像保罗所说的那样,jQuery将一个带有服务器内容类型json的空204响应视为错误。你可以通过手动覆盖dataType到'text'来解决它在jQuery中的问题。
$.ajax({
url: url,
dataType:'text',
success:(data){
//I will now fire on 204 status with empty content
//I have beaten the machine.
}
});
#5
2
This is alternative way to callback on success... I think that will work for you.
这是成功回调的另一种方式......我认为这对你有用。
$.ajax({
url: url,
dataType:'text',
statusCode: {
204: function (data) {
logic here
}
});
#1
11
204
should be treated as success. What version of jQuery are you using? I did a couple of tests and all 200 range status codes went to the success handler. The source for jQuery 1.4.2 confirms this:
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;
},
#2
9
I had a simliar issue because my script was also sending the "Content-Type" header as "application/json". While the request succeeded, it couldn't JSON.parse an empty string.
我有一个simliar问题,因为我的脚本也将“Content-Type”标题发送为“application / json”。当请求成功时,它无法JSON.parse一个空字符串。
#3
3
jQuery.ajax({
...
error: function(xhr, errorText) {
if(xhr.status==204) successCallback(null, errorText, xhr);
...
},
...
});
ugly...but might help
丑陋......但可能有所帮助
#4
3
It's technically a problem on your server
Like Paul said, an an empty 204 response with a server content-type of json is treated by jQuery as an error.
You can get around it in jQuery by manually overriding dataType to 'text'.
从技术上讲,它在你的服务器上是一个问题就像保罗所说的那样,jQuery将一个带有服务器内容类型json的空204响应视为错误。你可以通过手动覆盖dataType到'text'来解决它在jQuery中的问题。
$.ajax({
url: url,
dataType:'text',
success:(data){
//I will now fire on 204 status with empty content
//I have beaten the machine.
}
});
#5
2
This is alternative way to callback on success... I think that will work for you.
这是成功回调的另一种方式......我认为这对你有用。
$.ajax({
url: url,
dataType:'text',
statusCode: {
204: function (data) {
logic here
}
});