简体   繁体   English

jQuery.ajax在IE中带有“删除”方法的问题

[英]Problem with jQuery.ajax with 'delete' method in ie

I have a page where the user can edit various content using buttons and selects that trigger ajax calls. 我有一个页面,用户可以在其中使用按钮编辑各种内容,并选择触发ajax调用的页面。 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. 特别是,一个动作导致一个带有一些数据和一个“ put”请求的U​​RL远程调用,这(如我使用的是宁静的rails后端)触发了我的更新动作。 I also have a delete button which calls the same url but with a 'delete' request. 我还有一个删除按钮,它调用相同的URL,但带有“删除”请求。 The 'update' ajax call works in all browsers but the 'delete' one doesn't work in IE. “更新” ajax调用在所有浏览器中都有效,但“删除”一个在IE中不起作用。 I've got a vague memory of encountering something like this before...can anyone shed any light? 我之前碰到过类似这样的事情,记忆犹新……谁能阐明任何想法? here's my ajax calls: 这是我的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). 我将警报成功和错误放入删除ajax中,只是为了了解发生了什么,并且触发了ajax调用的“错误”部分,但是后端没有任何提示(我通过观察后端知道这一点服务器日志)。 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. 我不知道为什么-我从错误块中得到的'msg'是空白的。

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. Rails(也许还有其他框架?)对不支持的放置和删除请求有一个替代:将参数“ _method”(请注意下划线)设置为“ put”或“ delete”的发布请求将被视为实际请求类型是那个字符串。 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 will now treat this as if it were a delete request, preserving the REST system. 现在,Rails会将其视为删除请求,以保留REST系统。 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. 我的PUT示例起作用的原因仅仅是因为在这种特殊情况下IE很高兴发送PUT请求,但它正式不支持它们,因此最好对PUT请求和DELETE请求都这样做。

IE 7 and 8 do not support DELETE and PUT methods. IE 7和8不支持DELETE和PUT方法。 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.) 我遇到了一个问题,即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'}

Take a look at your type attribute type:'delete' 看一下您的type属性type:'delete'

jQuery documentation on type : 关于jQuery的文档类型

The type of request to make ("POST" or "GET"), default is "GET". 发出请求的类型(“ POST”或“ GET”),默认为“ GET”。 Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers. 注意:其他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'},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM