jQuery 发送字符串作为 POST 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5046930/
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 send string as POST parameters
提问by Mirgorod
I want to send a string as an ajax Post parameter.
我想发送一个字符串作为 ajax Post 参数。
The following code:
以下代码:
$.ajax({
type: "POST",
url: "http://nakolesah.ru/",
data: 'foo=bar&ca$libri=no$libri',
success: function(msg){
alert('wow'+msg);
}
});
Is not working. Why?
不管用。为什么?
回答by Darin Dimitrov
Try like this:
像这样尝试:
$.ajax({
type: 'POST',
// make sure you respect the same origin policy with this url:
// http://en.wikipedia.org/wiki/Same_origin_policy
url: 'http://nakolesah.ru/',
data: {
'foo': 'bar',
'ca$libri': 'no$libri' // <-- the $ sign in the parameter name seems unusual, I would avoid it
},
success: function(msg){
alert('wow' + msg);
}
});
回答by Chakavak Behzad
$.ajax({
type: 'POST',
url:'http://nakolesah.ru/',
data:'foo='+ bar+'&calibri='+ nolibri,
success: function(msg){
alert('wow' + msg);
}
});
回答by vitaly goji
I see that they did not understand your question. Answer is: add "traditional" parameter to your ajax call like this:
我看到他们没有理解你的问题。答案是:将“传统”参数添加到您的 ajax 调用中,如下所示:
$.ajax({
traditional: true,
type: "POST",
url: url,
data: custom ,
success: ok,
dataType: "json"
});
And it will work with parameters PASSED AS A STRING.
它将与作为字符串传递的参数一起使用。
回答by Dylan Valade
For a similar application I had to wrap my data
object with JSON.stringify()
like this:
对于类似的应用程序,我必须像这样包装我的data
对象JSON.stringify()
:
data: JSON.stringify({
'foo': 'bar',
'ca$libri': 'no$libri'
}),
The API was working with a REST client but couldn't get it to function with jquery ajax in the browser. stringify was the solution.
API 正在使用 REST 客户端,但无法在浏览器中使用 jquery ajax 使其正常工作。stringify 是解决方案。
回答by egnarts-ms
Not sure whether this is still actual.. just for future readers. If what you really want is to pass your parameters as part of the URL, you should probably use jQuery.param().
不确定这是否仍然是真实的......仅供未来的读者使用。如果您真正想要的是将参数作为 URL 的一部分传递,您可能应该使用 jQuery.param()。
回答by LCJ
Not a direct answer to your question.. But following is the only syntax that used to work for me -
不是您问题的直接答案..但以下是过去对我有用的唯一语法-
data: '{"winNumber": "' + win + '"}',
And the parameter-name match with the argument of the server method
并且参数名与服务器方法的参数匹配
回答by Sohel Pathan
I was facing the problem in passing string value to string parameters in Ajax. After so much googling, i have come up with a custom solution as below.
我在将字符串值传递给 Ajax 中的字符串参数时遇到了问题。经过如此多的谷歌搜索,我想出了一个自定义解决方案,如下所示。
var bar = 'xyz';
var calibri = 'no$libri';
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "http://nakolesah.ru/",
data: '{ foo: \'' + bar + '\', zoo: \'' + calibri + '\'}',
success: function(msg){
alert('wow'+msg);
},
});
Here, barand calibriare two string variables and you can pass whatever string value to respective string parameters in web method.
在这里,bar和calibri是两个字符串变量,您可以在 web 方法中将任何字符串值传递给相应的字符串参数。
回答by CodeLover
I have also faced this exact problem. But I have got a solution and it worked perfectly. I have needed to pass the parameters which are already produced by javascript function. So below code is working for me. I used ColdFusionfor the backend. I just directly used the parameters as a variable.
我也遇到过这个确切的问题。但我有一个解决方案,它工作得很好。我需要传递已经由 javascript 函数生成的参数。所以下面的代码对我有用。我使用ColdFusion作为后端。我只是直接将参数用作变量。
$.ajax({
url: "https://myexampleurl.com/myactionfile.cfm",
type: "POST",
data : {paramert1: variable1,parameter2: variable2},
success: function(data){
console.log(data);
} )};