javascript - Ajax 成功响应不起作用。无法在 IE11 上传递数据

标签 javascript php json ajax

抱歉,标题太血腥了,我对这个有点有点不知所措,并且已经尝试了我可以在网上找到的所有内容。我正在尝试通过帖子将数据传递到我的 sendjs.php 文件,但遇到问题。

  1. ajax success 函数不应用 if 语句。

此处的实时网站:www.diysoakwells.com.au(您可以添加项目并结帐进行测试)。

老实说,我什至不知道从哪里开始,因此我们将不胜感激,并且我将根据要求更新帖子并提供任何信息。

app.js

$(function() {


    // Get the form.
    var form = $("#ajax-contact");

    // Get the messages div.
    var formMessages = $("#form-messages");

    var spinner = $("#spinner");

    var submit = $("#submit");

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

        //display the cog animation
        $(spinner).removeClass("hidden");
        //hide the submit button
        $(submit).addClass("hidden");



        jsonObj=[];
        for(i=1;i<$(".item-price").length;i++)
        {
            var items={};
            var itemname = $(".item-name").get(i);
            var itemprice = $(".item-price").get(i);
            var itemquantity = $(".item-quantity").get(i);
            var itemtotal = $(".item-total").get(i);
            items["name"] = itemname.innerHTML;
            items["price"] = itemprice.innerHTML;
            items["quantity"] = itemquantity.innerHTML;
            items["total"] = itemtotal.innerHTML;
            jsonObj.push(items);

        }

        console.log(items);

        var formdata = {};
        formdata["textbox"] = $("#textbox").val();
        formdata["name"] = $("#name").val();
        formdata["phone"] = $("#phone").val();
        formdata["email"] = $("#email").val();
        formdata["address"] = $("#address").val();
        formdata["grandtotal"] = simpleCart.grandTotal();
        var x = 
        {
            "cart" : jsonObj,
            "formdata" : formdata,
            "captchaResponse" : $("#g-recaptcha-response").val()
        };
        //jsonString = jsonObj+formdata;
        var y = JSON.stringify(x);
        console.log(y);
        var result = jQuery.parseJSON(y);
        console.log(result);


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

        // Submit the form using AJAX.
        $.ajax({
            type: "post",
            url: "sendjs.php" ,
            //url: $(form).attr("action"),
            data: y,
            contentType: "application/json; charset=utf-8",
            traditional: true,
            success: function (response) {
                        if(response=="Thank You! Your message has been sent.")
                        {
                    //remove the button animation
                    $(spinner).addClass("hidden");
                    $(formMessages).removeClass("error");
                    $(formMessages).addClass("success");
                    $("#textbox").val("");
                    $("#name").val("");
                    $("#email").val("");
                    $("#message").val("");
                    $("#phone").val("");
                    $("#address").val("");

                        }
                        else
                        {
                    $(formMessages).removeClass("success");
                    $(formMessages).addClass("error");
                    $(spinner).addClass("hidden");
                    $(submit).removeClass("hidden");
                        }

                $(formMessages).text(response);

            }
        });
    });

});

sendjs.php

<?php
//Debugging
//ini_set( 'display_errors', 1 );
//error_reporting( E_ALL );

//replaces file_get_contents due to restrictions on server
function get_data($url)
    {
      $ch = curl_init();
      $timeout = 5;
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;
    }

    //turn url_fopen on due to restrictions on server
    //ini_set('allow_url_fopen', true);

    date_default_timezone_set('Australia/Perth');
    $time = date ("h:i A"); 
    $date = date ("l, F jS, Y");
    $json = file_get_contents('php://input');
    $obj = json_decode($json,true);
    $captcha = $obj["captchaResponse"];
    $captcha;
    $secretKey = "scrubbed";
    $ip = $_SERVER['REMOTE_ADDR'];
    $response = get_data("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);

    //not used due to server restictions
    //$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);

    $responseKeys = json_decode($response,true);

    if(intval($responseKeys["success"]) !== 1) {
        echo "Please Click on the Captcha";
        return false;
    }
    else 
    {
    //echo $items;
    $name = $obj["formdata"]["name"];
    $phone = $obj["formdata"]["phone"];
    $email = $obj["formdata"]["email"];
    $textbox = $obj["formdata"]["textbox"];
    $address = $obj["formdata"]["address"];
    $grandtotal  = $obj["formdata"]["grandtotal"];
    $text = "<html style='font-family:arial'>
    <body>
        <h1 style='color:crimson;'>DIY Soakwells</h1>
        <p>This order was submitted from www.diysoakwells.com.au on $date at $time</p>
        <p>$name thank you for your order inquiry. Deliveries are normally every Friday, we will be in contact shortly to discuss your order and confirm a time.</p>
        <p>An invoice will be issued after delivery for payment via bank transfer.</p>
        <p>In the meantime if you haven't already seen it, you can take a look at www.soakwellcalculator.com.au to confirm the number of soakwells you ordered will be adequate.</p>
        <br>

        <h2 style='color:crimson;'>CUSTOMER DETAILS</h2>
        <p><b>Email:</b>\n$email</p>
        <p><b>Name:</b>\n$name</p>
        <p><b>Phone:</b>\n$phone</p>
        <p><b>Delivery Address:</b>\n$address</p>
        <p><b>Message:</b>\n$textbox</p>
        <br>

        <h2 style='color:crimson;'>ORDER DETAILS</h2>";

        $items_in_cart = count($obj["cart"]);
        for($i=0; $i < $items_in_cart; $i++) {
            $iname = $obj["cart"][$i]["name"];
            $price = $obj["cart"][$i]["price"]; 
            $quantity = $obj["cart"][$i]["quantity"];
            $total = $obj["cart"][$i]["total"];
            //display looped cart data      
            $items .= "<p>$iname x $quantity - $price <small>ea.</small> <b>Sub Total: </b> $total .</p>";
        }

        $final_total ="<br>
        <p><b>Total: </b>$$grandtotal <small>inc. GST</small></p>
        </body>
        </html>";

        //Email Content
        $body = $text.$items.$final_total;

        // Set the email subject.
        $subject = "New order from $name";

        // Build the email content.
        $email_content = $body;

        // Build the email headers.
        $email_headers = 'MIME-Version: 1.0' . PHP_EOL;
        $email_headers .= 'Content-Type: text/html; charset=ISO-8859-1' . PHP_EOL;
        //$email_headers .= 'To:' . $name . '<' . $email . '>' . PHP_EOL;
        $email_headers .= 'From: DIY Soakwells <orders@diysoakwells.com>' . PHP_EOL;
        $email_headers .= 'CC: orders@diysoakwells.com.au' . PHP_EOL;
        $email_headers .= 'Reply-To: DIY Soakwells <orders@diysoakwells.com.au>' . PHP_EOL;
        $email_headers .= 'Return-Path: DIY Soakwells <orders@diysoakwells.com>' . PHP_EOL;
        $email_headers .= 'X-Sender: DIY Soakwells <orders@diysoakwells.com.au' . PHP_EOL;
        $email_headers .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
        //$email_headers .= 'X-Priority: 1' . PHP_EOL;



        //validate Email
        $email_check = filter_input(INPUT_POST, $email, FILTER_VALIDATE_EMAIL);
        //Recipients
        $to = $email;

        if (mail($to, $subject, $email_content, $email_headers, '-forders@diysoakwells.com.au')) {
            // Set a 200 (okay) response code.
            //http_response_code(200);
            echo "Thank You. Your order has been sent and a copy mailed to your inbox.";
        } else {
            // Set a 500 (internal server error) response code.
            //http_response_code(500);
            echo "There appears to be an issue with our server, please ring 0420 903 950 or email contact@diysoakwells.com.au.";
        }      
    }       
?>

希望有人能给我一些建议。

最佳答案

因为您的条件是响应==“谢谢!您的消息已发送。”

你的结果是 “请点击验证码”,“谢谢。您的订单已发送,副本已邮寄到您的收件箱。”,“我们的服务器似乎出现问题,请调用 0420 903 950 或发送电子邮件至 contact@ diysoakwells.com.au。”。

所以你的所有结果都会做其他部分

关于javascript - Ajax 成功响应不起作用。无法在 IE11 上传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45992241/

相关文章:

javascript - 如何设置全宽 (100%) 和未知高度的图像样式,使其不会弹出

javascript - 如何分解 JavaScript 中的正则表达式

php - Facebook PHP SDK 4.0 : Getting Long Term Access Token

python - 如何以多线程方式将 Python Dict 转换为 JSON

javascript - 在 webpack.config.js 中使用扩展运算符的语法错误

javascript - 使单页 Web 应用程序可被社交网络 (Facebook/Twitter/reddit) 抓取

php - 在管理员批准的任何支付网关中进行交易

php - 将图像存储在mysql数据库中

json - 隐藏 JSON 中的属性

javascript - 如何将坐标的 JSON 数据提供给 turf.polygon?