簡體   English   中英

我無法在HTML頁面中顯示Ajax請求響應

[英]I can't show the ajax Request Response in my html page

伙計們,我正在嘗試使用Ajax,當我單擊一個按鈕時,我將請求發送到服務器,然后它必須返回一些HTML並顯示它,並在div中顯示id =“ 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();
}
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
}

你可以試試看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM