我收到报告说,我开发的一个网站在IE 9和IE 10中没有正常运行。尝试提交表单时出现问题:
$("form[name='signIn']").submit(function(e) {
var formData = new FormData($(this)[0]);
e.preventDefault();
$( "#return_status_sign_in" ).empty();
$.ajax({
url: "<?= SITE_URL ?>account/login",
type: "POST",
data: formData,
async: false,
success: function (msg) {
$('#return_status_sign_in').append(msg);
},
cache: false,
contentType: false,
processData: false
});
});
以上内容通过AJAX在所有其他浏览器中提交表单,效果非常好。但是,在IE 9和10中,页面会刷新,POST数据会在URL中显示为get变量。这是怎么发生的?可能是e.preventDefault();是否未触发?如果是的话,还有什么替代方案?
正如我在评论中所说,IE 9使用"xdomainrequest"对象进行跨域请求,并使用"xmlhttprequest"进行其他请求。下面是我用来解决这个问题的代码示例xdomainrequests"仅发送"纯文本他们不能发送JSON:
if ('XDomainRequest' in window && window.XDomainRequest !== null) {
var xdr = new XDomainRequest(),
data = JSON.stringify(jsonData);
xdr.open('POST', 'http://www.yourendpoint.com');
xdr.onload = function() {
// When data is recieved
};
// All of this below have to be present
xdr.onprogress = function() {};
xdr.ontimeout = function() {};
xdr.onerror = function() {};
// Send the request. If you do a post, this is how you send data
xdr.send(data);
} else {
$.ajax({
url: 'http://www.yourendpoint.com',
type: 'POST',
dataType: 'json',
data: {
// your data to send
},
cache: true
})
.done(function(data) {
// When done
})
.fail(function(data) {
// When fail
});
}