2011-02-18 32 views
71

我想发送一个字符串作为ajax Post参数。jQuery发送字符串作为POST参数

下面的代码:

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

不工作。为什么?

+2

我看到你是一名PHP开发人员,我也看到你这样做:`ca $ libri = no $ libri`。只是检查确定在这里......你确定你不是不小心尝试使用JS结构的应用程序?如果你想在这个字符串中包含`$ libri`变量的值,试试这个:`'foo = bar&ca'+ $ libri +'= no'+ $ libri`。 – treeface 2011-02-18 22:03:52

+0

nono :)我了解通过php生成js的所有时刻:)它是APS中ajax变量的名称。我创建了解析器,用ajax解析某个站点。现在我得到了为什么错误。这是真正的跨域查询。我必须首先创建查询到我的服务器:) – Mirgorod 2011-02-18 22:10:45

回答

144

尝试这样的:这个

$.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); 
    } 
}); 
+1

不应该是数据:{foo:'bar'},没有撇号'? – 2013-03-21 10:39:46

+6

@MariusStanescu,两者都是等效的等效JavaScript语法。 – 2013-03-21 13:32:19

+3

也很确定CA $ libri中的$是完美无缺的 – 2015-02-02 20:38:16

27
$.ajax({ 
    type: 'POST',  
url:'http://nakolesah.ru/', 
data:'foo='+ bar+'&calibri='+ nolibri, 
success: function(msg){ 
    alert('wow' + msg); 
     } 
    }); 
4

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

1

不是直接回答你的问题。但下面是用来为我工作的唯一语法 -

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

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

8

对于类似的应用程序,我不得不换我data对象与JSON.stringify()这样的:

的API用REST客户合作但无法在浏览器中使用jquery ajax。 stringify是解决方案。

7

我看到他们不理解你的问题。 答案是:加“传统”的参数,你Ajax调用是这样的:

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

它将与字符串来传递参数工作。

0

我在将字符串值传递给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); 
    }, 
}); 

这里,酒吧宋体是两个字符串变量,你可以传递任何字符串值在Web方法各自的字符串参数。