dpi96151 2010-04-14 22:11
浏览 55
已采纳

Internet Explorer ajax请求不返回任何内容

At the end of my registration process you get to a payment screen where you can enter a coupon code, and there is an AJAX call which fetches the coupon from the database and returns it to the page so it can be applied to your total before it is submitted to paypal. It works great in Firefox, Chrome, and Safari, but in Internet Explorer, nothing happens. The (data) being returned to the jQuery function appears to be null.

jQuery Post

function applyPromo() {
var enteredCode = $("#promoCode").val();
$(".promoDiscountContainer").css("display", "block");
$(".promoDiscount").html("<img src='/images/loading.gif' alt='Loading...' title='Loading...' height='18' width='18' />");
$.post("/ajax/lookup-promo.php", { promoCode : enteredCode },
   function(data){
        if ( data != "error" ) {
            var promoType = data.getElementsByTagName('promoType').item(0).childNodes.item(0).data;
            var promoAmount = data.getElementsByTagName('promoAmount').item(0).childNodes.item(0).data;
            $(".promoDiscountContainer").css("display", "block");
            $(".totalWithPromoContainer").css("display", "block");
            if (promoType == "percent") {
                $("#promoDiscount").html("-" + promoAmount + "%");
                var newPrice = (originalPrice - (originalPrice * (promoAmount / 100)));
                $("#totalWithPromo").html(" $" + newPrice);
                if ( promoAmount == 100 ) {
                    skipPayment();
                }
            }
            else {
                $("#promoDiscount").html("-$" + promoAmount);
                var newPrice = originalPrice - promoAmount;
                $("#totalWithPromo").html(" $" + newPrice);
            }
            $("#paypalPrice").val(newPrice + ".00");
            $("#promoConfirm").css("display", "none");
            $("#promoConfirm").html("Promotion Found");
            finalPrice = newPrice;
        }
        else {
            $(".promoDiscountContainer").css("display", "none");
            $(".totalWithPromoContainer").css("display", "none");
            $("#promoDiscount").html("");
            $("#totalWithPromo").html("");
            $("#paypalPrice").val(originalPrice + ".00");
            $("#promoConfirm").css("display", "block");
            $("#promoConfirm").html("Promotion Not Found");
            finalPrice = originalPrice;
        }
   }, "xml");

}

Corresponding PHP Page

include '../includes/dbConn.php';

$enteredCode = $_POST['promoCode'];

$result = mysql_query( "SELECT * FROM promotion WHERE promo_code = '" . $enteredCode . "' LIMIT 1");

$currPromo = mysql_fetch_array( $result );

if ( $currPromo ) {
    if ( $currPromo['percent_off'] != "" ) {
        header("content-type:application/xml;charset=utf-8");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
        echo "<promo>";
        echo "<promoType>percent</promoType>";
        echo "<promoAmount>" . $currPromo['percent_off'] . "</promoAmount>";
        echo "</promo>";
    }
    else if ( $currPromo['fixed_off'] != "") {
        header("content-type:application/xml;charset=utf-8");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
        echo "<promo>";
        echo "<promoType>fixed</promoType>";
        echo "<promoAmount>" . $currPromo['fixed_off'] . "</promoAmount>";
        echo "</promo>";
    }
}
else {
    echo "error";
}

When I run the code in IE, I get a javascript error on the Javascript line that says

var promoType = data.getElementsByTagName('promoType').item(0).childNodes.item(0).data;

Here's a screenshot of the IE debuggeralt text

Update

I set a breakpoint on the line, and it actually encounters the error on this line:

if ( data != "error" ) {

HOWEVER, when I allow it to run the rest of the code after the breakpoint, it WORKS. I'm thinking that maybe IE is running the callback function before the ajax request is actually finished, so the PHP hasn't returned its results yet? Is there any way I can test that?

  • 写回答

1条回答 默认 最新

  • doouzlrvb01417498 2010-04-14 22:54
    关注

    Problem solved.

    The issue was that the PHP was returning an XML document, and the line data != "error" wasn't valid for an xml document, since an object can't be equal to a string. I changed the php to return XML for all cases, and changed the jQuery to read it differently and now it works beautifully. Thanks everyone for helping me talk it out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛