带有DELETE的$ .ajax丢失参数 [英] $.ajax with DELETE loses parameters

查看:73
本文介绍了带有DELETE的$ .ajax丢失参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用javascript和jQuery编写的客户端应用程序中-我有一个函数,该服务器通过DELETE方法对服务器执行$ .ajax请求.

In my client app—written in javascript and jQuery—I have a function where I'm doing $.ajax request with the DELETE method to my server.

代码是这样的:

    this.delete_one = function(id){
    console.log(id);
    $.ajax({
        url: sitesCtrl.url,
        type: "delete",
        dataType: 'json',
        data: {"id": id},
        success: function(data){
            if (data.success){
                $("sitesList").remove("#" + id + "\"");
            }
            else{
                console.log(data.message);
            }
        },
        error: function(){
            console.log("internal error");
        }
    })
};

问题在于服务器获取的请求没有参数"id"!只是一个简单的DELETE(根据firebug).使用PUT,POST或GET效果很好.

The problem is that the server gets the request with no parameter "id"! Just a simple DELETE (according to firebug). with PUT, POST, or GET it works great.

推荐答案

有趣.我在 RFC 中找不到任何内容,但是毫无疑问,没有理由使用DELETE方法传递参数-仅在GET和POST中传递,因此JQuery或浏览器可以正确过滤掉参数.不过,这只是一个猜测,也许一个真正知道这些东西的人可以做出更深刻的陈述.

Interesting. I can't find anything in the RFC but it stands to reason that there is no way to pass parameters using the DELETE methods - only in GET and POST, so either JQuery or the browser correctly filter out the parameters. This is just a guess, though, maybe somebody who knows this stuff by heart can make a more profound statement.

无论如何,如果这就是JQuery现在的工作方式,我认为您的解决方法将必须将ID放入URL中,并进行mod_rewrite.

Anyway, if this is how JQuery works right now, I think your workaround will have to be putting the ID into the URL, and mod_rewrite it out.

在执行此操作之前,请尝试是否通过在URL中添加参数 来欺骗浏览器以使其通过:sitesCtrl.url+'?ID='+id

Before you do that, try whether you can't trick the browser in passing it through by adding the parameter to the URL: sitesCtrl.url+'?ID='+id

这篇关于带有DELETE的$ .ajax丢失参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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