我无法让ajaxSubmit捕获错误:
$(document).ready(function(){
$("#supportForm").validate({
rules: {
//validation
},
messages: {
//messages
},
submitHandler: function(form) {
$(form).ajaxSubmit({
url:"ajax/supportform",
type:"GET",
dataType: 'json',
error: function() { alert('error'); },
success: function() {alert('success'); },
});
}
});
})
Run Code Online (Sandbox Code Playgroud)
我必须在我的PHP脚本中返回什么才能使其触发错误事件?
我试过返回一个错误=> 0的数组,退出(json_encode('error'=>'0');等。
请参阅Mike de Klerk的链接,其中提供了有关如何捕获错误的线索:
似乎成功和错误回调与传递布尔错误/成功消息无关,但仅当成功调用了url时才如此。我希望我不会收到错误结果,因为我使用的CMS总是返回某种内容-即使它是404或403页面。
无论如何,我必须从我的PHP脚本返回一个json字符串:
<?php
$response = json_encode(array("error" => "false","message" => "this is the error message"));
return $response;
Run Code Online (Sandbox Code Playgroud)
然后在我的成功回调中解析它:
submitHandler: function(form) {
$(form).ajaxSubmit({
url:"ajax/supportform",
type:"POST",
dataType: 'json',
error: function(data ) { alert(data); },
success: function(response) {
var obj = jQuery.parseJSON(response);
if(obj.error == "true"){
alert('error is true');
}else{
alert('error is false')
}
},
});
}
Run Code Online (Sandbox Code Playgroud)