Ajax调用永远不会成功

Iam facnigg和ajax问题,我在联系表单上工作,我遇到了ajax调用的问题,即使字段都输入正确,调用永远不会被取消。 我尝试了一些东西,但没有什么工作,Somene可以给我一个建议..?

非常感谢。

$.ajax({ url: "assets/contact_form/process-contact.php", type: "POST", dataType: "html", data: userData, beforeSend: function () { $( sb ).html('Sending...'); }, success: function (response) { if (response == true) { setTimeout(function () { window.location = 'index.html'; }, 1000); } else { $( sb ).html('Can not send, please try latter'); //IT GOES STRIGHT TO HERE } } }); } }); 

这是PHP代码:

  2 && strlen($userData['name'])  2 ){ $subject = 'Message from ' . $userData['name'] ; $message_phone= $userData['message'].'Phone number: '.$userData['phone']; $headers = 'From: ' . $userData['email']. "\r\n"; mail($my_email, $subject, $message_phone, $headers); // send mail // Save data to db (DON'T FORGET TO CLEAN AGAINST SQL INJECTIONS!!!) echo true; }}}} 

这可能有效。

 ...... if (response) { setTimeout(function () { window.location = 'index.html'; ..... 

如果你可以返回一个对象会更好:

 echo json_encode(['success' => true]); 

然后检查您的成功回调将如下所示:

 if (response.success){ //Your logic } 

希望这可以帮助。