本文介绍了Ajax调用永远不会成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam facnigg并遇到ajax问题,我正在处理联系表单,而ajax调用存在问题,即使字段都正确键入,该呼叫也不会成功.我尝试了几件事,但没有任何效果Somene可以给我建议..

Iam facnigg and issue with ajax, Im working on a contact form and I have an issue with the ajax call,The call is never succseed, even if the field are all typed correctly.I tried few things but nothing is workingSomene can give me an advice..?

非常感谢.

$.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代码:

<?php

  $userData['name'] = filter_var(trim($_POST['name']),
    FILTER_SANITIZE_STRING);
     $userData['email'] = filter_var(trim($_POST['email']),
   FILTER_VALIDATE_EMAIL);
      $userData['phone'] = filter_var(trim($_POST['phone']),
  FILTER_SANITIZE_STRING);
 $userData['message'] = filter_var(trim($_POST['message']),
 FILTER_SANITIZE_STRING);

 if( ! in_array( null, $userData ) ){

$my_email = '[email protected]';


$nameRegx = "/^[' a-zא-ת]+(\s[' a-zא-ת]+)*$/i";
 $phoneRegx = "/^(?:0(?!(5|7))(?:2|3|4|8|9))(?:-?\d){7}$|^(0(?=5|7)(?:-?\d)
{9})$/";

  if( strlen($userData['name']) > 2 &&  strlen($userData['name']) < 70 &&
 preg_match($nameRegx, $userData['name']) ) {

   if( preg_match($phoneRegx, $userData['phone']) ){

  if( strlen($userData['message']) > 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;

          }}}}

推荐答案

最好返回一个对象:

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

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

Then the check in your success callback will looks like :

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

希望这会有所帮助.

这篇关于Ajax调用永远不会成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:18