How to send json in post body with jQuery mobile - ajax

Please help me, what is wrong in this code?
$.ajax({
type: 'POST',
url: baseUrl+url,
data: {language: 'it'},
xhrFields: {
withCredentials : true
}
})
why server receives:
'language=it_IT'

Try to specify your dataType and use JSON.stringify():
$.ajax({
type: 'POST',
url: baseUrl+url,
data: JSON.stringify ({language: 'it'}),
xhrFields: {
withCredentials : true
},
contentType: "application/json",
dataType: 'json'
})

I just run through the same issue: for some reason, whenever you send language parameter using ajax it somehow automatically changes to get and all post parameters get lost. Solution: avoid using language parameter at all (or stringify yout data as #Agash Thamo suggested. That is strange for me and I would really like if somoeno could explain that a little bit better.

Related

Is it possible to add multi media type to HTTP header?

I want to make a request have both JSON and file
My controller :
#Authorize(roles={UserType.ADMIN,UserType.SCHOOL_ADMIN})
#RequestMapping(value="import",method=RequestMethod.POST)
public List<AddUserResponse>importUserBundle(#RequestBody AddUserRequest test,#RequestParam(value="userCsv")MultipartFile[] userCsv)
And got error when making a request :
Content type
'multipart/form-data;boundary=----WebKitFormBoundary62tvsTfonhCQ6HSl;charset=UTF-8'
not supported
Is there any way to make a request with both multipart/form-data and application/json media type?
Your AJAX call should be like this:
$.ajax({
url: '/importUserBundle',
type: 'POST',
data: {
userCsv: FileToSend,
test: JsonData
},
dataType: 'json',
processData: false,
contentType: false,
success: function(data)
{
}
});
EDITED
processData:false Don't process the files
contentType:false Set content type to false as jQuery will tell
the server its a query string request.
Look at this for more info http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax
Hope this will help

Meteor HTTP.call and jquery ajax

I would like to use Meteor.call('GET') instead of $.ajax(). I have an ajax call as the following:
$.ajax({
url: url,
crossDomain:true,
type: method,
data: query,
dataType: 'json'
}).done(function(data) {
_tokens.request = {
token: data.oauth_token,
secret: data.oauth_token_secret.split('')
};
});
Have some options but I don't know how to pass to Meteor.call(). Please help!
Thank you so much
You probably mean HTTP.call or HTTP.get. Meteor.call is related to another concept.
http://docs.meteor.com/#/full/http_get

Ajax serialise - issue with data format

I have several inputs formatted with this jquery plugin here.
I use $.ajax to do my mysql insert:
$.ajax({
type: 'GET',
url: 'xxxx.php',
data: $('#new_form').serialize(),
}),
I face an issue as my input values are formatted with the plugins and can't get into mysql db.
As an example:
Input value: $450,000.00 is not accepted.
Is there a way to unformat within the serialise function values that have a specific classes (like class="money")?
Thanks for your help!
I have tried the below code:
$.ajax({
type: 'GET',
url: 'xxx.php',
data: $('#new_form').serialize(),
dataType:"json",
beforeSend: function(){
$(".money").cleanVal();
},
<script>
function cleanVal(v) {
return v.replace(/^\,/,'');
};
</script>
the result of the insert in mysql is still 450 for 450,000.
Do you have an idea?
thanks
You can try using the plugin $.cleanVal() method to retrieve the unmasked type value of the corresponding HTML element, prior to your AJAX form submission. So something like this:
$.ajax({
type: 'GET',
url: 'xxxx.php',
data: $('#new_form').serialize(),
beforeSend: function(){
$(".money").cleanVal();
}
}),
I couldn't make it work with beforehand. I found a solution which is to unmask values before calling ajax.
If anyone knows why it does not work with beforesend, thanks for letting me know.
cheers

Alternative method to ajax json

I develop a web solution for a company and I want to get php variables to my pages using ajax. The problem is that the server of that company is somewhat old and I cannot use json using jason_encode for that. Is there any alternative method to do that without using json? Help would be really appreciated.
formData = {
// all your parameters here
param1: param1,
param2: param2
}
$.ajax({
type: 'POST',
contentType: 'application/json',
url: "https://www.example.com/test",
dataType: "json",
data: formData,
success: function(data) {
//success handling
},
error: function(data) {
//error handling
}
});

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

Resources