jQuery发送字符串作为POST参数

Mir*_*rod 90 ajax jquery post

我想发送一个字符串作为ajax Post参数.

以下代码:

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

不管用.为什么?

Dar*_*rov 174

试试这样:

$.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);
    }
});
Run Code Online (Sandbox Code Playgroud)

  • @MariusStanescu,两者都是等效的javascript语法. (6认同)
  • 也很确定ca $ libri中的$非常好 (3认同)

Cha*_*zad 37

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

  • 这就是我来到这里的原因,以了解如何将$ .post数据作为字符串发送.接受的答案对我没有任何帮助.谢谢. (15认同)

vit*_*oji 12

我看到他们不明白你的问题.答案是:将"传统"参数添加到您的ajax调用中,如下所示:

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

并且它将使用参数PASSED AS A STRING.


Dyl*_*ade 9

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

data: JSON.stringify({ 
  'foo': 'bar', 
  'ca$libri': 'no$libri'
}),
Run Code Online (Sandbox Code Playgroud)

API正在使用REST客户端,但无法在浏览器中使用jquery ajax.stringify是解决方案.