我使用Ajax调用来发出POST请求,然后调用API。然后返回结果。我最初使用的是启动的成功回调,但由于某种原因,它停止了工作,我读到它已经被替换为done,所以我尝试更改它。我不知道为什么,但是现在既没有成功也没有成功。我最近更新了浏览器,所以这可能和它有关。我也在Wordpress中运行它。200将在“网络”选项卡中返回,状态和消息编码为JSON。jQuery版本为1.12.4。虽然Ajax调用完成了,但是没有任何登录控制台,并且对话框没有打开。我怎么才能让这个起作用?
$.ajax({
type: 'POST',
dataType: 'json',
url: ajax_object.ajax_url,
data: {
'action': $action,
'product_id': $product_id,
},
success: function(data) {
console.log(data.message);
$( "#dialog" ).text(data.message);
$( "#dialog" ).dialog( "option", "title", "API - " + data.status );
$( "#dialog" ).dialog("open");
}
});
$.ajax({
type: 'POST',
dataType: 'json',
url: ajax_object.ajax_url,
data: {
'action': $action,
'product_id': $product_id,
}
}).done(function(result) {
console.log(result.message);
$( "#dialog" ).text(result.message);
$( "#dialog" ).dialog( "option", "title", "API - " + result.status );
$( "#dialog" ).dialog("open");
});
发布于 2019-07-02 09:38:58
从您的dataType中删除jQuery参数,这是AJAX在触发.done()之前使用的JSON转换失败的原因,因为在jQuery中缺少了一些东西/错误。您可以通过在.fail()之后添加.done()来检查它,您将看到它是如何通过错误触发的。
$.ajax({}).done().fail(error => console.log(error)
jQuery.ajax
试图根据指定的dataType
参数或服务器发送的内容类型标头来转换响应体。如果转换失败(例如,如果JSON/XML无效),则会触发错误回调。
Ajax request returns 200 OK, but an error event is fired instead of success
https://stackoverflow.com/questions/56849310
复制相似问题