开发者

jQuery send string as POST parameters

开发者 https://www.devze.com 2023-02-12 12:56 出处:网络
I want to send a string as an ajax Post parameter. The following code: $.ajax({开发者_开发技巧 type: \"POST\",

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?


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


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


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.


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.


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


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消