02-27-2013 04:43 PM
Hi, I want to consume a web service using ajax. I need to use a post method but when I do it, the web services doesn't receive the params that i'm sending.
My code is:
$.ajax({
type : "POST",
url : url,
data : "{id_user='"+id+"',edad='22',ciudad='Lima',sexo='s s',token='"+accessToken+"'}",
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(data) {
alert("success");
},
error : function (xhr) {
if (xhr.status == 400)
console.log("400 "+xhr.responseText, xhr.status);
else
console.log(xhr.responseText, xhr.status);
}
});
I also tried jsonp but i couldn't make a post method with it.
Anyone knows how can i send a json post or how you do a post method in webworks???
Please!!!!
03-01-2013 10:04 AM
This bit is all wrong...
data : "{id_user='"+id+"',edad='22',ciudad='Lima',sexo='s s',token='"+accessToken+"'}",
You're sending a string
What you want is...
data : { id_user : id, edad : 22, ciudad : 'Lima', sexo : 'ss', token : accessToken },
03-01-2013 06:58 PM - edited 03-01-2013 06:59 PM
Assuming you're using jQuery for the ajax call:
var url = "your webservices endpoint";
var data = ({id_user: id, edad: 22, ciudad:"Lima", sexo: "ss", token: accessToken});
$.ajax({
type : "POST",
url : url,
data : $.params(data),
dataType : "json",
success : function(data) {
alert("success");
},
error : function (err) {
console.log(JSON.stringify(err));
}
});