what im trying to do is to send data in JSON using jquery to server and receive it at server side. After that parse it and use the data and again response back to client in json form. Im using Play(web based framework for java) at server side. Here is my code:
Tier.js
var attribs={
id: document.forms["tier"]["id"].value ,
name: document.forms["tier"]["name"].value ,
limits: document.forms["tier"]["limits"].value
};
$.ajax({
type:"get",
url: "validateTier.json" ,
dataType: "json",
jsonp: 'callback',
data: attribs,
contentType: "application/json; charset=utf-8",
beforeSend: function( xhr ) {
xhr.overrideMimeType( 'application/json; charset=utf-8' );
},
error:function(x,e){
if(x.status==0){
alert('You are offline!!\n Please Check Your Network.');
}else if(x.status==404){
alert('Requested URL not found.');
}else if(x.status==500){
alert('Internel Server Error.');
}else if(e=='parsererror'){
alert('Error.\nParsing JSON Request failed.');
}else if(e=='timeout'){
alert('Request Time out.');
}else {
alert('Unknow Error.\n'+x.responseText);
}
},
success: function(data) {
alert(data.status);
},
}).done(function( msg ) {
alert( "Data Saved: " + msg );
}); // End ajax
at Server side:
Map<String,Object> json = new HashMap<String,Object>();
json.put("status", status);
json.put("message", msg);
renderJSON(json);
Output:
You are offline!!
Please Check Your Network.
It looks to me like you are not actually sending the data to the server as JSON – you appear to just be sending arguments as typical name/value pairs. If your server backend is expecting JSON, that is likely the source of your error.