使用 jquery 进行 POST,如何正确提供“数据”参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3066070/
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
Using jquery to make a POST, how to properly supply 'data' parameter?
提问by user246114
I'd like to make an ajax call as a POST, it's going to go to my servlet. I want to send parameterized data, like the following:
我想将 ajax 调用作为 POST,它将转到我的 servlet。我想发送参数化数据,如下所示:
var mydata = 'param0=some_text¶m1=some_more_text';
I supply this as the 'data' parameter of my jquery ajax() call. So this should be inserted in the body of the POST, right? (I mean, not appended to my 'mysite/save' url?):
我将此作为我的 jquery ajax() 调用的“数据”参数提供。所以这应该插入到POST的正文中,对吗?(我的意思是,没有附加到我的“我的网站/保存”网址?):
$.ajax({
url: 'mysite/save',
type: 'POST',
data: mydata
});
it appears to work correctly. In my servlet, I am just dumping all received parameters, and I see them all come through nicely:
它似乎工作正常。在我的 servlet 中,我只是转储所有接收到的参数,我看到它们都很好地通过:
private void printParams(HttpServletRequest req) {
Enumeration paramNames = req.getParameterNames();
while (paramNames.hasMoreElements()) {
// print each param key/val here.
}
}
also, I should url encode my data string manually before use, right? Like:
另外,我应该在使用前手动对我的数据字符串进行 url 编码,对吗?喜欢:
var mydata = 'param0=' + urlencode('hi there!');
mydata += '¶m1=' + urlencode('blah blah');
mydata += '%param2=' + urlencode('we get it');
Thanks!
谢谢!
回答by Nick Craver
An easier way is to provide the data
property as an object, like this:
更简单的方法是将data
属性作为对象提供,如下所示:
$.ajax({
url: 'mysite/save',
type: 'POST',
data: { param0:'hi there!', param1:'blah blah', param2:'we get it' }
});
Otherwise, yes you should encode it, as anything with an &
for example would screw things up very quickly. Providing it as an object is a much cleaer/simpler approach though, in my opinion anyway.
否则,是的,您应该对其进行编码,因为任何带有&
例如的东西都会很快搞砸。无论如何,在我看来,将它作为一个对象提供是一种更清晰/更简单的方法。
You can also space it out, and retrieve properties from other places inline, like this:
您还可以将其隔开,并从其他位置内联检索属性,如下所示:
$.ajax({
url: 'mysite/save',
type: 'POST',
data: {
param0: $('#textbox0').val(),
param1: $('#textbox1').val(),
param2: $('#textbox2').val()
}
});
Edit:If you're curious how jQuery does this encoding internally, it's using $.param()
(which you can use directly as well) to encode the object to a string, called here, and the guts here.
编辑:如果您对 jQuery 如何在内部进行这种编码感到好奇,它正在使用$.param()
(您也可以直接使用)将对象编码为一个字符串,在此处调用,并在此处调用。
回答by Vivin Paliath
If you have a form, you can also do var data = jQuery("#myForm").serialize();
which puts it in a form that jQuery.ajax
can understand and use. Otherwise, use the object literal described in Nick's answer.
如果你有一个表格,你也var data = jQuery("#myForm").serialize();
可以把它放在一个jQuery.ajax
可以理解和使用的表格中。否则,请使用 Nick 的回答中描述的对象文字。
回答by Amr Badawy
can this help you
这可以帮助你吗
function CallPageSync(url, data) {
var response;
$.ajax({
async: false,
url: url,
data: data,
timeout: 4000,
success: function(result) {
response = result;
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
response = "err--" + XMLHttpRequest.status + " -- " + XMLHttpRequest.statusText;
}
});
return response;
}
and you can call it like
你可以这样称呼它
response = CallPageSync(checkPageURL, "un=" + username);
回答by Adel Mourad
You need this part:
你需要这部分:
data: JSON.stringify({
BarcodeNumber: $('#shipmentId-input').val(),
StatusId: $('[name="StatusId"]').val()
}),
Full object :
完整对象:
$.ajax(
{
url: "/Agent/Shipment/BulkUpdate",
method: "POST",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
BarcodeNumber: $('#shipmentId-input').val(),
StatusId: $('[name="StatusId"]').val()
}),
success: function (data, textStatus, jqXHR) {
if (textStatus== "success") {
alert(data);
// Handel success
}
else {
// Handel response error
}
},
error: function (jqXHR, textStatus, errorThrown){
//Handel connection error
}
});
回答by Jubair
You don't need to URL encode POST variables. However if you are interacting with a database, you will want to make sure your variables are injection attack protected.
您不需要对 POST 变量进行 URL 编码。但是,如果您正在与数据库交互,则需要确保您的变量受到注入攻击保护。
What you have will work, however as Vivinmentioned, if it's a form then the best way to do it is via .serialize()
.
您所拥有的将起作用,但是正如Vivin 所提到的,如果它是一种形式,那么最好的方法是通过.serialize()
.
I use this a LOT personally for all my form submissions (done via .ajax()
).
我个人经常使用它来提交所有表单(通过 完成.ajax()
)。