无法发出Ajax发布请求

时间:2013-12-01 12:50:08

标签: javascript php mysql ajax

对不起伙计们,我的PHP代码有问题。所以它看起来像ajax.status === 0通常是php或asp问题。你走了:

我写了

require_once($DOCUMENT_ROOT/"mysql.php");

而不是

require_once("$DOCUMENT_ROOT/mysql.php");

我也用过

$mysql->user 

而不是

$mysql->username 

我在mysql.php中写道 [/编辑]

嗨我正在调用两个函数从window.onload加载的函数内部发出ajax请求,当我尝试时,我总是收到ajax.status === 0和 500内部网络错误从companySponsors()发出请求(另一个函数,即.documentData()工作得很好)。我该如何处理这种情况?

window.onload = init;

function init() {
    "use strict";

    companySponsors();
    try {
        U.addEvent(U.I("paymentButton"), "click", calculate);
        U.addEvent(U.I("recordButton"), "click", recordData);
        U.addEvent(U.I("loanAmount"), "change", calculate);
        U.addEvent(U.I("interest"), "change", calculate);
        U.addEvent(U.I("repayment"), "change", calculate);
        U.addEvent(U.I("zipcode"), "change", calculate);
    } catch (err) {
        console.log("Error: " + err.name + " on file " + err.filename + " at line number " + err.lineno + " with message " + err.message);
    }
}

function recordData() {
    "use strict";

    var loanAmount = parseFloat(U.I("loanAmount").value);
    var interest = parseFloat(U.I("interest").value);
    var payments = parseFloat(U.I("repayment").value);
    var zipcode = U.I("zipcode").value;

    var ajax = getXMLHttpRequest();

    try {
        testAjax(ajax);
    } catch (err) {
        console.log("Error: " + err.name + " on file " + err.fileName + " at line number " + err.lineNumber + " with message " + err.message);
    }

    var url = "php/recordData.php";

    ajax.open("POST", url, true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    ajax.onload = function () {
        if (ajax.status === 200) {
            if (ajax.responseText !== "\nsuccess\n") {
                alert("Could not record the loan data");
            } else {
                alert("The loan data was successfully recorded");
            }
        }
    };

    ajax.send("amt=" + encodeURIComponent(loanAmount) + "&ai=" + encodeURIComponent(interest) + "&yrs=" + encodeURIComponent(payments) + "&zip=" + encodeURIComponent(zipcode));
};

function companySponsors() {
    "use strict";

    var ajax = getXMLHttpRequest();
    try {
        testAjax(ajax);
    } catch (err) {
        console.log("Error: " + err.name + " on file " + err.fileName + " at line number " + err.lineNumber + " with message " + err.message);
    }

    ajax.open("POST", "php/sponsors.php", true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


    ajax.onload = function () {
        if (ajax.status === 200) {
            var lenders = JSON.parse(ajax.responseText);
            var list = "";
            for (var i = 0; i < lenders; i++) {
                var lender = lenders[i];
                list += "<li><a href='" + lender.url + "'>" + lender.name + "</a></li>";
            }
            U.I("ad").innerHTML = "<ul>" + list + "</ul>";
        }
    };

    ajax.send(null);
};

0 个答案:

没有答案
相关问题