我在客户端使用了 jQuery.ajax() 方法。
在服务器端,PHP 脚本return false;
- 这通常会触发在客户端执行的error 函数。但目前这种情况不再发生了。执行success函数并且data = ""
。
如果 PHP 脚本返回 false,jQuery.ajax() 方法不应该引发 error 方法吗?
$.ajax({
url: "././mail/contact.php",
type: "POST",
data: {
name: name,
subject: subject,
email: email,
message: message
},
cache: false,
success: function(data, textStatus, jqXHR) {
// Success message
},
error: function(jqXHR, textStatus, errorThrown) {
// Fail message
},
});
<小时/>
更新:在服务器端 PHP 脚本中调用 mail();
函数。如果失败的话,是否会将 http 状态代码设置为 500?
最佳答案
$.ajax({
...
error: function(jqXHR, textStatus, errorThrown) {
// Fail message
},
});
error
回调函数是在 HTTP 错误时调用的,而不是在代码中出现逻辑错误时调用的。来自 the doc ,
error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )
A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besidesnull
) are"timeout"
,"error"
,"abort"
, and"parsererror"
. When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as"Not Found"
or"Internal Server Error"
这意味着error
回调函数将在以下情况下被触发:
- 您尚未连接到任何经过验证的网络
- 找不到请求的页面
- 存在任何内部服务器错误
- 您的 AJAX 请求处理时间太长
- AJAX 请求因某种原因被中止等。
所以你的问题,
Inside the server-side PHP script the mail(); function is called. Does this one set the http status code perhaps to 500 if it fails ?
没有。一般来说,即使您的 mail()
函数失败,它也不会将 HTTP 状态代码设置为 500,当然,除非处理时间太长,在这种情况下您会收到超时错误在 error
回调函数中。
关于javascript - jQuery ajax 请求,PHP 返回 false 但没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44863483/