0

我使用这个 JavaScript 代码...

function data_addproduct(id){
var hr   = new XMLHttpRequest();
var url  = "scripts/data_processing.php";
var data = "addproduct";
var vars = "data="+data+"&id="+id;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        document.getElementById("ui_checkout").innerHTML = return_data;
        document.getElementById("ui_checkout_mobile").innerHTML = return_data;
        data_refreshtotal();
        data_refreshproducts();
    }
}
hr.send(vars);
document.getElementById("ui_checkout").innerHTML = "<br>Updating...";
document.getElementById("ui_checkout_mobile").innerHTML = "<br>Updating...";

}

但是在 iOS 上的 Safari 中,它没有调用其他两个函数......

data_refreshtotal();
data_refreshproducts();

这很奇怪,因为它在 Chrome for iOS 中运行良好,有人知道如何在 Safari 中修复它吗?

它调用的函数:

function data_refreshtotal(){   
var hr   = new XMLHttpRequest();
var url  = "scripts/data_processing.php";
var data = "refreshtotal";
var vars = "data="+data;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        document.getElementById("ui_total").innerHTML = return_data;
        document.getElementById("ui_total_mobile").innerHTML = return_data;
        document.getElementById("ui_checkout_notify").innerHTML = "Online EPOS";
    }
}
hr.send(vars);
document.getElementById("ui_total").innerHTML = "Updating...";
document.getElementById("ui_total_mobile").innerHTML = "Updating...";
document.getElementById("ui_checkout_notify").innerHTML = "Updating...";
}
function data_refreshproducts(){    
var hr   = new XMLHttpRequest();
var url  = "scripts/data_processing.php";
var data = "refreshproducts";
var vars = "data="+data;

hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        document.getElementById("ui_checkout").innerHTML = return_data;
        document.getElementById("ui_checkout_mobile").innerHTML = return_data;
        document.getElementById("ui_checkout_notify").innerHTML = "Online EPOS";
    }
}
hr.send(vars);
document.getElementById("ui_checkout").innerHTML = "<br>Updating...";
document.getElementById("ui_checkout_mobile").innerHTML = "<br>Updating...";
document.getElementById("ui_checkout_notify").innerHTML = "Updating...";
}
4

1 回答 1

0

从您发布的内容来看,唯一有意义的是(以某种方式)data_refreshtotal并且data_refreshproducts在它们被调用时已经超出范围。这是您的相同代码,经过重构和命名空间,以确保每个函数在您调用它时仍在范围内:

var Cart;
if (!Cart) {
    Cart = {};
}
(function () {
    'use strict';
    Cart.stringificate = function stringificate(data) {
        var key = '',
            postData = '';
        for (key in data) {
            if (data.hasOwnProperty(key)) {
                postData += '&' + key + '=' + data[key];
            }
        }
        return postData.substring(1);
    };
    Cart.ajax = function ajax(options) {
        var hr = new XMLHttpRequest(),
            method = options.method ? options.method.toUpperCase() : "POST",
            url = options.url || "scripts/data_processing.php",
            i = 0;
        hr.open(method, url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function () {
            if (hr.readyState === 4 && hr.status === 200) {
                options.callback(hr.responseText);
            }
        };
        hr.send(Cart.stringificate(options.data));
        for (i = 0; i < options.notices.length; i += 1) {
            document.getElementById(options.notices[i]).innerHTML = "<br />Updating&hellip;";
        }
    };
    Cart.data_refreshtotal = function data_refreshtotal() {
        Cart.ajax({
            "data": {
                "data": "refreshtotal"
            },
            "callback": function (return_data) {
                document.getElementById("ui_total").innerHTML = return_data;
                document.getElementById("ui_total_mobile").innerHTML = return_data;
                document.getElementById("ui_checkout_notify").innerHTML = "Online EPOS";
            },
            "notices": [
                "ui_total",
                "ui_total_mobile",
                "ui_checkout_notify"
            ]
        });
    };
    Cart.data_refreshproducts = function data_refreshproducts() {
        Cart.ajax({
            "data": {
                "data": "refreshproducts"
            },
            "callback": function (return_data) {
                document.getElementById("ui_checkout").innerHTML = return_data;
                document.getElementById("ui_checkout_mobile").innerHTML = return_data;
                document.getElementById("ui_checkout_notify").innerHTML = "Online EPOS";
            },
            "notices": [
                "ui_checkout",
                "ui_checkout_mobile",
                "ui_checkout_notify"
            ]
        });
    };
    Cart.data_addproduct = function data_addproduct(id) {
        Cart.ajax({
            "data": {
                "data": "addproduct",
                "id": id
            },
            "callback": function (return_data) {
                document.getElementById("ui_checkout").innerHTML = return_data;
                document.getElementById("ui_checkout_mobile").innerHTML = return_data;
                Cart.data_refreshtotal();
                Cart.data_refreshproducts();
            },
            "notices": [
                "ui_checkout",
                "ui_checkout_mobile"
            ]
        });
    };
}());

我使用Cart命名空间只是因为它看起来好像你正在制作一个购物车(而且它可以快速输入:))。尝试运行它并查看函数是否被调用。

于 2012-10-25T00:29:28.490 回答