I was doing the Spring MVC and NetBeans Rest service from database tutorial
http://netbeans.org/kb/docs/websvc/rest.html#rest-spring
But now wanted to consume the service with an ajax request in jquery:
var data = {"id":"1","identification":"1010182323","name":"jhon"};
$.ajax({
url: "/notes/"
,type: 'POST'
,dataType: "json"
,data: data
,success: function(data, textStatus, jqXHR){
},
error: function(jqXHR, textStatus, errorThrown){
}
});
And I was getting the error:
415 Unsupported Media Type
The solution was specifying the contentType and encoding the data to json:
var data = {"id":"1","identification":"1010182323","name":"jhon"};
$.ajax({
url: "/notes/"
,type: 'POST'
,dataType: "json"
,data: Ext.JSON.encode(data) // here
,contentType: "application/json" // here
,success: function(data, textStatus, jqXHR){
},
error: function(jqXHR, textStatus, errorThrown){
}
});
Here is an example of how to use the Ext.JSON.encode method
http://jhonjairoroa87.blogspot.com/2012/10/javascripy-simple-encodedecode-json.html
Resources:
http://docs.oracle.com/cd/E19226-01/820-7627/giepu/index.html
http://www.checkupdown.com/status/E415_es.html
http://stackoverflow.com/questions/9754767/cannot-set-content-type-to-application-json-in-jquery-ajax
http://api.jquery.com/jQuery.ajax/
No comments:
Post a Comment