Javascript jQuery.ajax 问题与 ie 中的“删除”方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2456820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem with jQuery.ajax with 'delete' method in ie
提问by Max Williams
I have a page where the user can edit various content using buttons and selects that trigger ajax calls. In particular, one action causes a url to be called remotely, with some data and a 'put' request, which (as i'm using a restful rails backend) triggers my update action. I also have a delete button which calls the same url but with a 'delete' request. The 'update' ajax call works in all browsers but the 'delete' one doesn't work in IE. I've got a vague memory of encountering something like this before...can anyone shed any light? here's my ajax calls:
我有一个页面,用户可以在其中使用按钮编辑各种内容并选择触发 ajax 调用的内容。特别是,一个操作会导致远程调用 url,其中包含一些数据和“放置”请求,这(因为我使用的是宁静的 Rails 后端)触发我的更新操作。我还有一个删除按钮,它调用相同的 url 但带有“删除”请求。“更新”ajax 调用适用于所有浏览器,但“删除”调用不适用于 IE。我之前遇到过这样的事情,我有一个模糊的记忆......任何人都可以透露任何信息吗?这是我的 ajax 调用:
//update action - works in all browsers
jQuery.ajax({
async:true,
data:data,
dataType:'script',
type:'put',
url:"/quizzes/"+quizId+"/quiz_questions/"+quizQuestionId,
success: function(msg){
initializeQuizQuestions();
setPublishButtonStatus();
}
});
//delete action - fails in ie
function deleteQuizQuestion(quizQuestionId, quizId){
//send ajax call to back end to change the difficulty of the quiz question
//back end will then refresh the relevant parts of the page (progress bars, flashes, quiz status)
jQuery.ajax({
async:true,
dataType:'script',
type:'delete',
url:"/quizzes/"+quizId+"/quiz_questions/"+quizQuestionId,
success: function(msg){
alert("success");
initializeQuizQuestions();
setSelectStatus(quizQuestionId, true);
jQuery("tr[id*='quiz_question_"+quizQuestionId+"']").removeClass('selected');
},
error: function(msg){
alert("error:" + msg);
}
});
}
I put the alerts in success and error in the delete ajax just to see what happens, and the 'error' part of the ajax call is triggered, but WITH NO CALL BEING MADE TO THE BACK END (i know this by watching my back end server logs). So, it fails before it even makes the call. I can't work out why - the 'msg' i get back from the error block is blank.
我在删除 ajax 中设置了成功和错误警报只是为了看看会发生什么,并且触发了 ajax 调用的“错误”部分,但没有调用后端(我通过观察我的后端知道这一点)服务器日志)。因此,它甚至在发出呼叫之前就失败了。我不知道为什么 - 我从错误块中返回的“味精”是空白的。
Any ideas anyone? Is this a known problem? I've tested it in ie6 and ie8 and it doesn't work in either.
任何人的想法?这是一个已知问题吗?我已经在 ie6 和 ie8 中测试过它,但它在两者中都不起作用。
thanks - max
谢谢 - 最大
EDIT - the solution - thanks to Nick Craver for pointing me in the right direction.
编辑 - 解决方案 - 感谢 Nick Craver 为我指明了正确的方向。
Rails (and maybe other frameworks?) has a subterfuge for the unsupported put and delete requests: a post request with the parameter "_method" (note the underscore) set to 'put' or 'delete' will be treated as if the actual request type was that string. So, in my case, i made this change - note the 'data' option':
Rails(也许还有其他框架?)对不受支持的 put 和 delete 请求有一个诡计:参数“_method”(注意下划线)设置为“put”或“delete”的 post 请求将被视为实际请求类型是那个字符串。因此,就我而言,我进行了此更改 - 请注意“数据”选项:
jQuery.ajax({
async:true,
data: {"_method":"delete"},
dataType:'script',
type:'post',
url:"/quizzes/"+quizId+"/quiz_questions/"+quizQuestionId,
success: function(msg){
alert("success");
initializeQuizQuestions();
setSelectStatus(quizQuestionId, true);
jQuery("tr[id*='quiz_question_"+quizQuestionId+"']").removeClass('selected');
},
error: function(msg){
alert("error:" + msg);
}
});
}
Rails will now treat this as if it were a delete request, preserving the REST system. The reason my PUT example worked was just because in this particular case IE was happy to send a PUT request, but it officially does not support them so it's best to do this for PUT requests as well as DELETE requests.
Rails 现在会将其视为删除请求,从而保留 REST 系统。我的 PUT 示例工作的原因只是因为在这种特殊情况下 IE 很乐意发送 PUT 请求,但它正式不支持它们,因此最好对 PUT 请求和 DELETE 请求执行此操作。
采纳答案by Nick Craver
Take a look at your type attribute type:'delete'
看看你的 type 属性 type:'delete'
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.
要发出的请求类型(“POST”或“GET”),默认为“GET”。注意:这里也可以使用其他 HTTP 请求方法,如 PUT 和 DELETE,但并非所有浏览器都支持。
I would instead try and include this with your data and look for it on the server-side, like this:
我会尝试将其包含在您的数据中并在服务器端查找它,如下所示:
data: {'action': 'delete'},
回答by PeppyHeppy
IE 7 and 8 do not support DELETE and PUT methods. I had a problem where IE7,8 would not follow a 302 redirect and IE would use the DELETE or PUT method for the location that it was supposed to redirect to (with a get.)
IE 7 和 8 不支持 DELETE 和 PUT 方法。我遇到了一个问题,IE7,8 不会遵循 302 重定向,并且 IE 会使用 DELETE 或 PUT 方法来定位它应该重定向到的位置(使用 get。)
To ensure that IE7 and 8 work properly, I would use a POST with the parameters:
为了确保 IE7 和 8 正常工作,我将使用带有参数的 POST:
data: {'_method': 'delete'}

