jQuery.ajax 在 ie 中使用“删除"方法的问题 [英] Problem with jQuery.ajax with 'delete' method in ie

查看:26
本文介绍了jQuery.ajax 在 ie 中使用“删除"方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,用户可以在其中使用按钮编辑各种内容并选择触发 ajax 调用的内容.特别是,一个操作会导致远程调用 url,其中包含一些数据和放置"请求,这(因为我使用的是宁静的 Rails 后端)触发我的更新操作.我还有一个删除按钮,它调用相同的 url 但带有删除"请求.更新"ajax 调用适用于所有浏览器,但删除"调用不适用于 IE.我之前遇到过这样的事情,我有一个模糊的记忆......任何人都可以透露任何信息吗?这是我的 ajax 调用:

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:

//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);
      }
    });     
  }

我在删除 ajax 中设置了成功和错误的警报只是为了看看会发生什么,并且触发了 ajax 调用的错误"部分,但是没有调用到后端(我通过观看知道这一点我的后端服务器日志).因此,它甚至在发出呼叫之前就失败了.我不知道为什么 - 我从错误块中返回的味精"是空白的.

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.

任何人有任何想法吗?这是一个已知问题吗?我已经在 ie6 和 ie8 中测试过它,但在两者中都不起作用.

Any ideas anyone? Is this a known problem? I've tested it in ie6 and ie8 and it doesn't work in either.

谢谢 - 最大

编辑 - 解决方案 - 感谢 Nick Craver 为我指明了正确的方向.

EDIT - the solution - thanks to Nick Craver for pointing me in the right direction.

Rails(也许还有其他框架?)对不受支持的 put 和 delete 请求有一个诡计:参数_method"(注意下划线)设置为put"或delete"的 post 请求将被视为实际的请求类型是那个字符串.因此,就我而言,我进行了此更改 - 请注意数据"选项:

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':

   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 现在会将其视为删除请求,从而保留 REST 系统.我的 PUT 示例工作的原因只是因为在这种特殊情况下 IE 很乐意发送 PUT 请求,但它正式不支持它们,因此最好对 PUT 请求和 DELETE 请求执行此操作.

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.

推荐答案

看看你的类型属性 type:'delete'

jQuery 类型文档:

发出请求的类型(POST"或GET"),默认为GET".注意:这里也可以使用其他的HTTP请求方法,如PUT、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.

我会尝试将其包含在您的数据中并在服务器端查找,如下所示:

I would instead try and include this with your data and look for it on the server-side, like this:

data: {'action': 'delete'},

这篇关于jQuery.ajax 在 ie 中使用“删除"方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆