jQuery send string as POST parameters

AjaxPostJquery

Ajax Problem Overview


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?

Ajax Solutions


Solution 1 - Ajax

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

Solution 2 - Ajax

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

Solution 3 - Ajax

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.

Solution 4 - Ajax

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.

Solution 5 - Ajax

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

Solution 6 - Ajax

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

Solution 7 - Ajax

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

Solution 8 - Ajax

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.

Solution 9 - Ajax

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMirgorodView Question on Stackoverflow
Solution 1 - AjaxDarin DimitrovView Answer on Stackoverflow
Solution 2 - AjaxChakavak BehzadView Answer on Stackoverflow
Solution 3 - Ajaxvitaly gojiView Answer on Stackoverflow
Solution 4 - AjaxDylan ValadeView Answer on Stackoverflow
Solution 5 - Ajaxegnarts-msView Answer on Stackoverflow
Solution 6 - AjaxLCJView Answer on Stackoverflow
Solution 7 - AjaxCodeLoverView Answer on Stackoverflow
Solution 8 - AjaxSohel PathanView Answer on Stackoverflow
Solution 9 - AjaxAshwin BalaniView Answer on Stackoverflow