Reputation: 3829
How do I reproduce this JQuery AJAX call using only Dojo Toolkit?
$.ajax({
url: "${pageContext.request.contextPath}/api/user/get",
data: JSON.stringify(requestParam),
type: "POST",
beforeSend: function(xhr)
{
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(user)
{
}
});
I've tried using this code to no avail:
var xhrArgs =
{
url: "${pageContext.request.contextPath}/api/user/get",
postData: dojo.toJson(requestParam),
handleAs: "text",
load: function(user)
{
},
error: function(error)
{
}
}
var deferred = dojo.xhrPost(xhrArgs);
The server returns : 415 - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
I use Spring MVC on the server side if that matters.
Upvotes: 0
Views: 1029
Reputation: 10559
If the response from your server is going to be JSON, then Fikri is correct in that you will want to set handleAs
to "json"
. However, that's not your main problem right now.
In the jQuery version of your code, you are setting 2 headers. You don't seem to be setting those in the Dojo version of your code. To set headers, add a headers
property to your code:
var xhrArgs =
{
url: "${pageContext.request.contextPath}/api/user/get",
postData: dojo.toJson(requestParam),
handleAs: "json",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
load: function(user)
{
},
error: function(error)
{
}
}
Also, if you are using Dojo 1.8 or newer, use the new dojo/request
API instead.
require(["dojo/request", "dojo/json"], function (request, JSON) {
var promise = request.post("${pageContext.request.contextPath}/api/user/get", {
data: JSON.stringify(requestParam),
handleAs: "json",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
}).then(function (user) {
// load callback
}, function (error) {
// error callback
});
});
Upvotes: 2
Reputation: 359
My guess is the handleAs
key is set to text
. Try changing it to handleAs: 'json'
Upvotes: 0