1

我有一个非常简单的 ajax 调用,它会达到成功状态,但不会达到错误状态,它只会抛出 404。奇怪的是这在 IE 中工作得很好,但在 Firefox 或 Chrome 中却不行。如果我将数据类型更改为文本,它在 Firefox 中可以正常工作,但在 IE 中不能正常工作。任何帮助表示赞赏!

$.ajax({
    type: "GET",
    crossDomain: true,
    url: 'http://www.google.com',       
    contentType: 'application/json; charset=utf-8',
    dataType: 'jsonp',
    jsonp: 'callback'
}).success(function(){
    console.log("success");
}).error(function(xhr, ajaxOptions, thrownError) {
    console.log("error");
});
4

1 回答 1

0

不存在成功和错误。您需要调用 JS 函数完成并失败。此外,对于最佳实践,在失败情况下使用 console.error 而不是 console.log。

试试下面的代码:

$.ajax({
    type: "GET",
    crossDomain: true,
    url: 'http://www.google.com',       
    contentType: 'application/json; charset=utf-8',
    dataType: 'jsonp',
    jsonp: 'callback'
}).done(function(){
    console.log("success");
}).fail(function(xhr, ajaxOptions, thrownError) {
    console.error("error");
});
于 2020-05-28T19:23:56.110 回答