我正在构建一个Wordpress插件,我正在尝试使用AJAX。我无法让AJAX在IE中工作。
以下尝试在FF / Chrome中都有效但在IE中失败。
PHP:
add_action('wp_ajax_nonpriv_trying_ajax', 'trying_ajax');
add_action('wp_ajax_trying_ajax', 'trying_ajax');
function trying_ajax(){
$response = json_encode( array( 'success' => true ) );
header( "Content-Type: application/json" );
echo $response;
exit;
}
使用Javascript:
var ajaxData = {
action: 'trying_ajax',
state: filterStates
};
JQuery.post(myconvio_convio_functions.ajaxurl, ajaxData,
function(data) {
alert(data);
},
'json'
);
FF / Chrome会返回一个JSON对象;但IE返回0。
http://codex.wordpress.org/AJAX_in_Plugins 说...
错误返回值
如果请求网址是AJAX请求失败 wp-admin / admin-ajax.php,它将返回-1或0,具体取决于 它失败的原因。另外,如果AJAX请求成功,它 将返回0。
我试过这个来解释缓存:
var d = new Date();
var n = d.getTime();
JQuery.post(myconvio_convio_functions.ajaxurl+"?x="+n, ajaxData,
我尝试过相关网址来解决跨域问题:
JQuery.post('/FWW/wp-admin/admin-ajax.php', ajaxData,
所有相同的结果: FF / Chrome返回一个JSON对象;但IE返回0。
感谢您的帮助。
....更改为$ .ajax并添加成功,错误和完成 - 结果在评论中。 结果仍然相同,IE回归成功,但响应为0
$.ajax({
url: '/FWW/wp-admin/admin-ajax.php',
type: 'POST',
data: ajaxData,
dataType: 'json',
success: function(response, status, xhr) {
console.log(response);
// Firebug log: Object { success=true }
alert('success - response: ' + response);
// FF Alert: success - response: [object Object]
// IE Alert: success - response: 0
alert('success - status: ' + status);
// IE&FF Alert: success - status: success
alert('success - xhr.readyState: ' + xhr.readyState);
// IE&FF Alert: success - xhr.readyState: 4
alert('success - xhr.responseText: ' + xhr.responseText);
// FF Alert: success - xhr.responseText: {"success":true}
// IE Alert: success - xhr.responseText: 0
alert('success - xhr.status: ' + xhr.status);
// IE&FF Alert: success - xhr.status: 200
alert('success - xhr.statusText: ' + xhr.statusText);
// IE&FF Alert: success - xhr.statusText: OK
},
// error handler function never gets called on IE or FF
error: function(jqXHR, textStatus, errorThrown) {
alert('error jqXHR: ' + jqXHR);
alert('error textStatus: ' + textStatus);
alert('error errorThrown: ' + errorThrown);
},
complete: function(jqXHR, textStatus){
alert('complete - jqXHR.readyState: ' + jqXHR.readyState);
// IE&FF Alert: complete - jqXHR.readyState: 4
alert('complete - jqXHR.responseText: ' + jqXHR.responseText);
// FF Alert: complete - jqXHR.responseText: {"success":true}
// IE Alert: complete - jqXHR.responseText: 0
alert('complete - jqXHR.status: ' + jqXHR.status);
// IE&FF Alert: complete - jqXHR.status: 200
alert('complete - jqXHR.statusText: ' + jqXHR.statusText);
// IE&FF Alert: complete - jqXHR.statusText: OK
alert('complete - textStatus: ' + textStatus);
// IE&FF Alert: complete - textStatus: success
}
});