108

I want to send a string as an ajax Post parameter.

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?

xav
  • 5,452
  • 7
  • 48
  • 57
Mirgorod
  • 31,413
  • 18
  • 51
  • 63
  • 2
    I see that you are a PHP developer and I also see that you did this: `ca$libri=no$libri`. Just checking to be sure here...are you sure you're not accidentally trying to use PHP constructions where JS ones should be? If you want to include the value of the `$libri` variable into this string, try this: `'foo=bar&ca' + $libri + '=no' + $libri`. – treeface Feb 18 '11 at 22:03
  • nono :) i understand all moments of generating js by php :) it's the names of ajax variables in APS. I create parser that parse some site with ajax. And now i got why error. It's coz crossdomain query. I must to create query first to my server :) – Mirgorod Feb 18 '11 at 22:10

9 Answers9

194

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);
    }
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
43
$.ajax({
    type: 'POST',    
    url:'http://nakolesah.ru/',
    data:'foo='+ bar+'&calibri='+ nolibri,
    success: function(msg){
        alert('wow' + msg);
    }
});
Neuron
  • 5,141
  • 5
  • 38
  • 59
Chakavak Behzad
  • 807
  • 1
  • 9
  • 13
  • 16
    this is why I came here, to find out how to send $.post data as a string. the accepted answer does not at all help me with that. thank you. – chiliNUT Jun 27 '14 at 23:11
  • Agreed, I had a situation as well where a string was required for a framework I was developing, good answer. In my situation I was able to make this work by putting the string in a variable next to data:, the format of my string was '?var=value&var2=value2' – Joseph Astrahan Jun 16 '15 at 06:18
15

I see that they did not understand your question.

Answer is: add "traditional" parameter to your ajax call like this:

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

And it will work with parameters PASSED AS A STRING.

en0ndev
  • 673
  • 1
  • 7
  • 19
vitaly goji
  • 256
  • 4
  • 13
12

For a similar application I had to wrap my data object with JSON.stringify() like this:

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.

Dylan Valade
  • 5,565
  • 6
  • 42
  • 56
4

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().

egnarts-ms
  • 146
  • 6
1

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

LCJ
  • 22,196
  • 67
  • 260
  • 418
1

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.

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, bar and calibri are two string variables and you can pass whatever string value to respective string parameters in web method.

Sohel Pathan
  • 367
  • 3
  • 13
  • Meh. I don't recommend manually crafting a json string via string concatenation -- you become vulnerable to a variable breaking your string (creating an invalid json string). – mickmackusa Apr 28 '22 at 03:50
1

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 ColdFusion for the backend. I just directly used the parameters as a variable.

                    $.ajax({
                    url: "https://myexampleurl.com/myactionfile.cfm",
                    type: "POST",
                    data : {paramert1: variable1,parameter2: variable2},
                    success: function(data){
                        console.log(data);                              
                    } )};
CodeLover
  • 151
  • 1
  • 7
0

Instead of this, encode the POST request as a string and pass to the data parameter,

var requestData = "Param1=" + encodeURIComponent(jsParam1) + "&Param2="+ encodeURIComponent(jsParam2);

var request = $.ajax({
                            url: page + "?" + getVars,
                            method: "POST",
                            data: requestData,
                            dataType: "html",
                            contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
                    });
Ashwin Balani
  • 745
  • 7
  • 19