data.responseText is undefined when using jQuery ajax with iframe-transport
Legacy code that fails with jQuery > 1.9
$.ajax(app.bannerSettings.url, {
files: self.$("#upload-file"),
iframe: true
}).complete(function(data) {
if(data.status === 200)
{
var response = $.parseJSON(data.responseText);
(...)
}
else
{
notifier.error_x(data.responseText);
}
});
Corrected code for jQuery >= 1.10
$.ajax(app.bannerSettings.url, {
files: self.$("#upload-file"),
iframe: true,
dataType: "json"
}).complete(function(data) {
if(data.status === 200)
{
var response = $.parseJSON(data.responseText);
(...)
}
else
{
notifier.error_x(data.responseText);
}
});
Notice that the .complete() handler is used. In that case the responseText is available unparsed. Using complete() is suggested by the plugin author.
Plugin: jquery.iframe-transport.js
Sources: https://github.com/cmlenz/jquery-iframe-transport
Documentation: https://cmlenz.github.io/jquery-iframe-transport/
The documentation does not mention using dataType: "json". I tested using it with the old version of jQuery 1.9.1 and it still works so I would suggest updating the documentation to use dataType: "json" in all cases.
Related links on similar issues:
https://github.com/jakerella/jquery-mockjax/issues/95
Recent Comments