带参数的 jQuery Ajax PUT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8032938/
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
jQuery Ajax PUT with parameters
提问by andrei g
It seems that using jQuery Ajax POST will pass parameters, but PUT will not. I looked at the current jQuery code and PUT and DELETE are not there. I looked at 1.4.2 jQuery and PUT and DELETE are there.
似乎使用 jQuery Ajax POST 会传递参数,但 PUT 不会。我查看了当前的 jQuery 代码,但没有 PUT 和 DELETE。我看了 1.4.2 jQuery 和 PUT 和 DELETE 在那里。
What is the workaround for passing parameters with a PUT request using the current version of jQuery?
使用当前版本的 jQuery 通过 PUT 请求传递参数的解决方法是什么?
回答by Jayendra
Can you provide an example, because put should work fine as well?
您能否提供一个示例,因为 put 也应该可以正常工作?
Documentation -
文档 -
The type of request to make ("POST" or "GET"); the 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,但并非所有浏览器都支持。
Have the example in fiddle and the form parameters are passed fine (as it is put it will not be appended to url
) -
将示例放在小提琴中,并且表单参数传递得很好(因为它不会被附加到url
)-
$.ajax({
url: '/echo/html/',
type: 'PUT',
data: "name=John&location=Boston",
success: function(data) {
alert('Load was performed.');
}
});
Demotested from jQuery 1.3.2 onwards on Chrome.
演示在 Chrome 上从 jQuery 1.3.2 开始测试。
回答by ow3n
You can use the PUT method and pass data that will be included in the body of the request:
您可以使用 PUT 方法并传递将包含在请求正文中的数据:
let data = {"key":"value"}
$.ajax({
type: 'PUT',
url: 'http://example.com/api',
contentType: 'application/json',
data: JSON.stringify(data), // access in body
}).done(function () {
console.log('SUCCESS');
}).fail(function (msg) {
console.log('FAIL');
}).always(function (msg) {
console.log('ALWAYS');
});
回答by Dan Mandle
For others who wind up here like I did, you can use AJAX to do a PUT with parameters, but they are sent as the body, not as query strings.
对于像我一样在这里结束的其他人,您可以使用 AJAX 执行带有参数的 PUT,但它们作为正文发送,而不是作为查询字符串发送。
回答by user3623446
Use:
用:
$.ajax({
url: 'feed/4', type: 'POST', data: "_METHOD=PUT&accessToken=63ce0fde", success: function(data) {
console.log(data);
}
});
Always remember to use _METHOD=PUT
.
永远记得使用_METHOD=PUT
.