问题
I'm using mockjax to simulate an Ajax call, but I'm experiencing that the data received by my Ajax callback is different from what I pass to mockjax (via the 'responseText' parameter). In this example, I've chosen the response to be '14.0', but the callback receives '14' instead:
$.mockjax({
url: "/test",
contentType: "text/json",
responseText: "14.0"
});
$.ajax({
url: "/test",
datatype: "json"
}).done(function(data) {
alert(data);
});
Why is it that the received data is different from what I specify to responseText
? See this fiddle for a working example; a popup dialog will show the string received by the callback, should be '14'.
EDIT:
This is the popup I get when running the fiddle, demonstrating the altered response from mockjax.
Also fixed the fiddle.
回答1:
If you change two small things the above snippet will work as you expect.
In the above code snippet the contentType mentioned in $.mockjax is "text/json". In that case the responseText needs to be an object
that represents the JSON. https://github.com/appendto/jquery-mockjax
$.mockjax({
url: "/test",
contentType: "text/json",
responseText: { number: 14.0 }
});
Also, in the $.ajax call the datatype
key should be dataType
http://api.jquery.com/jquery.ajax/
$.ajax({
url: "/test",
dataType: "json"
}).done(function(data) {
console.log(data);
});
I've made the changes in the following jsFiddle http://jsfiddle.net/elijahmanor/BtuW8/
I hope that helps you past the issue.
来源:https://stackoverflow.com/questions/12093731/why-does-mockjax-return-an-altered-response-to-my-ajax-consumer