doutizhou5312 2015-03-03 18:56
浏览 43

Ajax失败,状态为0

My ajax request is always failing with status 0. The PHP file being called is executing all the code. I am unable to locate the reason why the request is failing.Here is the ajax code:

$(document).ready(function() {
// Get the form.
var form = $('#reg-form');

// Get the messages div.
var formMessages = $('#login_form');

// Set up an event listener for the contact form.
$(form).submit(function(event) {
    // Stop the browser from submitting the form.
    //e.preventDefault();

    // Serialize the form data.
    var formData = $(form).serialize();

    // Submit the form using AJAX.
    alert("Data submitted");
    var submitresponse = $.ajax({
        type: 'POST',
        url: "mailer.php",
        data: formData,
        dataType: "json",
        });
            submitresponse.done(function(response) {
            alert("Hi");
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');
            alert(response);    
            // Set the message text.
            $(formMessages).text(response);

            // Clear the form.
            //$('#name').val('');
            //$('#email').val('');
            //$('#Address').val('');
            });
                submitresponse.fail(function(xhr,status,response) {
                alert("Hii");
                console.log(arguments);
                alert(status);  
                // Make sure that the formMessages div has the 'error' class.
                $(formMessages).removeClass('success');
                $(formMessages).addClass('error');

                // Set the message text.
                //if (data.responseText !== '') {
                alert(xhr.responsetext);    
                //  $(formMessages).text(data.responseText);
                //} else {
                    //$(formMessages).text('Oops! An error occured and your message could not be sent.');
                //}
                $(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {

// This is the default error handler for ajax request.

// Extract all the information required to understand.
var requestResponse = {
url: ajaxSettings.url,
method: ajaxSettings.type,
data: ajaxSettings.data,
httpStatus: jqXHR.status,
error: thrownError || jqXHR.statusText,
data: ajaxSettings.data
};

console.error(requestResponse)
// Notify the user so he might not wonder.
alert('Something went wrong, Please try again');
});
                });
            });
});

The PHP Code is as follows:

  <?php
  $servername = "*";
  $username = "*";
  $password = "*";
  $dbname = "*";
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  if ($conn->connect_error) {
  //die("Connection failed: " . $conn->connect_error);
  die("Something went wrong. Please try again or contact front desk.           <br/>");
  }
 // Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("","
"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $address = trim($_POST["Address"]);
    $class = $_POST["class"];
    $school = $_POST["school"];
    $city = $_POST["City"];
    $fname = $_POST["f_name"];
    $mobileno = $_POST["mobileno"];
     // Check that data was sent to the mailer.
    if ( empty($name) OR empty($address) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }
   $sql = "INSERT INTO registrations(Name, Father_Name,Email,Class,School,Address,City,Mobile_No,Reg_time) values ('$name','$fname','$email','$class','$school','$address','$city',$mobileno,now())";
    if (!mysqli_query($conn,$sql))
 {
 die('Error: ' . mysqli_error($conn));
 }
 //echo "Your Information Was Successfully Posted"; 
 mysqli_close($conn);
       // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = $email;

    // Set the email subject.
    $subject = "ISMART Registration";

    // Build the email content.
    $email_content = "Name: $name
";
    $email_content .= "Email: $email

";
    $email_content .= "Message:
$address
";
    $email_content .= "Class: $class
";

    // Build the email headers.
    $email_headers = "From: ismart@asd.co.in";
    // Send the email.
    if (mail($recipient, $subject, $email_content,$email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
      echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>

Console log shows: Object {url: "mailer.php", method: "POST", data: "name=Nitish+Garg&f_name=sdfs&class=11&school=DAV+P…+1-A%2C+PWSSB+House%2C+Barakuan+Road&City=Patiala", httpStatus: 0, error: "error"} Please help!

  • 写回答

1条回答 默认 最新

  • dongqiao8502 2015-05-11 19:43
    关注

    httpStatus with a 0 means that the Ajax call isn't getting a complete response from the server. There's a disconnect somewhere and you're either not getting anything from the server or just not the full response. I'd check on timeouts first.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法