jQuery发送string作为POST参数

我想发送一个string作为ajax Post参数。

以下代码:

$.ajax({ type: "POST", url: "http://nakolesah.ru/", data: 'foo=bar&ca$libri=no$libri', success: function(msg){ alert('wow'+msg); } }); 

不pipe用。 为什么?

尝试像这样:

 $.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); } }); 
 $.ajax({ type: 'POST', url:'http://nakolesah.ru/', data:'foo='+ bar+'&calibri='+ nolibri, success: function(msg){ alert('wow' + msg); } }); 

对于类似的应用程序,我不得不用JSON.stringify()来包装我的data对象:

 data: JSON.stringify({ 'foo': 'bar', 'ca$libri': 'no$libri' }), 

API与REST客户端一起工作,但无法在浏览器中使用jquery ajax。 stringify是解决scheme。

我看到他们不明白你的问题。 答案是:像这样添加“传统”参数给你的ajax调用:

 $.ajax({ traditional: true, type: "POST", url: url, data: custom , success: ok, dataType: "json" }); 

它将使用参数作为一个string。

不知道这是否仍然是实际的..只为未来的读者。 如果你真正想要的是传递你的参数作为URL的一部分,你应该使用jQuery.param() 。

不是你的问题的直接答案..但以下是唯一的语法,曾经为我工作 –

 data: '{"winNumber": "' + win + '"}', 

参数名称与服务器方法的参数匹配

我在将string值传递给Ajax中的string参数时遇到了问题。 经过这么多的search,我已经想出了一个自定义的解决scheme,如下所示。

 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); }, }); 

在这里, barcalibri是两个stringvariables,您可以将任何string值传递给Web方法中的各个string参数。