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?

2
  • 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. Commented Feb 18, 2011 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 :) Commented Feb 18, 2011 at 22:10

9 Answers 9

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);
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

shouldn't it be data: { foo: 'bar' }, without the apostrophe ' ?
@MariusStanescu, both are equivalent equivalent javascript syntax.
also pretty sure the $ in the ca$libri is perfectly fine
Doesn't answer the question regarding posting as string :(, see below answer.
This works but this doesn't: foo: 'bar' . I have spent 2 days causeless I didn't add the strings signs to both sides!!!
43
$.ajax({
    type: 'POST',    
    url:'http://nakolesah.ru/',
    data:'foo='+ bar+'&calibri='+ nolibri,
    success: function(msg){
        alert('wow' + msg);
    }
});

2 Comments

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.
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'
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.

Comments

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.

2 Comments

Why we had to do this?
Not sure but some characters in the response must not be considered strings unless forced.
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().

Comments

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

Comments

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.

1 Comment

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.