[ajax] jQuery send string as POST parameters

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?

This question is related to ajax post jquery

The answer is


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.


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

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

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.


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.


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


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


Examples related to ajax

Getting all files in directory with ajax Cross-Origin Read Blocking (CORB) Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource Fetch API request timeout? How do I post form data with fetch api? Ajax LARAVEL 419 POST error Laravel 5.5 ajax call 419 (unknown status) How to allow CORS in react.js? Angular 2: How to access an HTTP response body? How to post a file from a form with Axios

Examples related to post

How to post query parameters with Axios? How can I add raw data body to an axios request? HTTP POST with Json on Body - Flutter/Dart How do I POST XML data to a webservice with Postman? How to set header and options in axios? Redirecting to a page after submitting form in HTML How to post raw body data with curl? How do I make a https post in Node Js without any third party module? How to convert an object to JSON correctly in Angular 2 with TypeScript Postman: How to make multiple requests at the same time

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?