response.statusText =“OK”但是使用jQuery从ajax进入错误回调
服务器端:
.... $_SESSION['accountId'] = $accountId; $_SESSION['type'] = $_POST['accountType']; echo '1'; return;
客户端:
$.ajax( { type:'POST', data: $("#flowFormSubmit").serialize(), dataType:'text/plain', timeout:1000, success:function(response){ if(-1 == response) alert('fail'); else alert('succeed'); } });
我已经测试过它在’返回’时就停止了 在什么条件下成功function不会被调用?
编辑 :
添加错误回调后,它被捕获,但尚未输出有用的信息:
error:function(response){ alert(response);alert(response.statusText); },
它只输出:
[object XMLHttpRequest] OK
为什么会出现错误回调?
指定URL选项可能是个好主意:
$.ajax( { url:'foo.php', type:'POST', ...
错误回调有更多有用的参数。 尝试获取textStatus。
error: function(XHR, textStatus, errorThrown) { console.log(textStatus); }
检查那是什么。
一个快速的猜测是你打算使用dataType: 'text'
而不是'text/plain'
。 ( 检查文件 )
$.ajax({ type:'POST', data: $("#flowFormSubmit").serialize(), dataType:'text', timeout:1000, success:function(response){ if(-1 == response) alert('fail'); else alert('succeed'); } });
尝试从服务器端代码中省略return语句。 就您的错误回调而言,使用以下通用ajax回调代码,让我们知道失败时status,responseText和statusText属性是什么:
function callback(response) { if (response.readyState == 4) { if (response.status == 200) { alert("Response Success:\n" + response.responseText); } else { alert("Response Error:\n" + response.statusText); } } }
你忘了吗:
header('Content-Type:text/plain');
在服务器上?
1)转到浏览器中的URL(使用ajax获取的URL)
2)为jquery ajax请求设置错误回调。
在您的PHP代码中,使用json_encode返回响应
例如:
$response = array('success'=>1); echo json_encode($response);
那么在你的ajax成功函数中它应该是
if (response.success == 1) { alert('success'); }