本文介绍了问题与jQuery.ajax与即“删除”的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,用户可以编辑各种使用内容的按钮,并选择触发Ajax调用。特别是,一个动作会导致一个URL来进行远程调用,有一些数据和一个放的要求,它(因为我使用的是宁静轨后端)触发我的更新操作。我也有一个删除按钮,调用相同的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调用的错误部分被触发,但(我知道没有要求被做后端此观看我的后端服务器日志)。因此,它失败之前就进行调用。我不明白为什么 - 的'味精'我从错误块背面是空白的。

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.

铁轨(?也许其他框架)有一个借口为不支持PUT和DELETE请求:与参数_method(注意下划线)设置为放或删除后请求将被视为实际的请求类型是字符串。所以,在我的情况,我做了这种变化 - 请注意数据选项:

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会现在把它当作好像它是一个删除请求,preserving的作息制度。我PUT例如工作的原因是只缘身在此特定情况下IE很高兴发送PUT请求,但官方不支持他们,所以最好做到这一点的PUT请求,以及删除请求。

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.

推荐答案

看看你的类型属性键入:删除

的jQuery的类型文档:

请求的类型,使(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'},

这篇关于问题与jQuery.ajax与即“删除”的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:08