我无法在我的html页面中显示ajax请求响应

时间:2017-03-30 12:55:22

标签: javascript jquery ajax

伙计们我正在尝试使用ajax,我在点击按钮时将请求发送到服务器,然后它必须返回一些HTML并显示它,在id =&#的div中34; formTag",但它没有工作可以得到一些建议, 她是我的代码:

var ajaxRequest; // The variable that makes Ajax possible!
function ajaxFunction() {
try {

    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e) {

    // Internet Explorer Browsers
    try {
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {

        try {
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {

            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
  }
}

function getForm(objButton) {
ajaxFunction();
if (ajaxRequest.readyState == 4) {
   document.getElementById("formTag").innerHTML = ajaxRequest.Response;
}
   var buttonValue = objButton.value;
   ajaxRequest.open("get", "get-form/" + buttonValue, true);
   ajaxRequest.send();
}

1 个答案:

答案 0 :(得分:1)

function getForm(objButton) {
    ajaxFunction();
    ajaxRequest.onreadystatechange=function{
        if (ajaxRequest.readyState == 4) {
            document.getElementById("formTag").innerHTML = ajaxRequest.responseText;//property responseText contain data from server
        }
    }
    var buttonValue = objButton.value;
    ajaxRequest.open("get", "get-form/" + buttonValue, true);
    ajaxRequest.send(null);//parameter null is necessary even you don't pass data
}

你可以试试这个。