如何使用ajax响应重写表

时间:2014-12-06 05:53:59

标签: javascript ajax jsp

我尝试使用javascript和ajax重写表。此处已发送请求并且响应已到达,但innerHTML无法使用响应文本重写表内容。 这是我的代码。

<table> <tbody id='product1'> <tr> <td> Product Name:</td> <td>P1</td> </tr> </tbody> </table>

的Javascript

function compareProd(prodID,tId){ browse(); var url = 'process.jsp'; url += '?compareProd='+prodID; xmlHttp.onreadystatechange = changeProd(tId); xmlHttp.open('POST', url, true); xmlHttp.send(null); } function changeProd(tID) { if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') { document.getElementById('product1').innerHTML = xmlHttp.responseText; } }

process.jsp <tr> <td> Product Name:</td> <td>P2</td> </tr>

此处响应已到达,但无法重写表格。

1 个答案:

答案 0 :(得分:0)

在单个函数中尝试并在假设您初始化xmlHttp请求对象的情况下进行检查。

xmlHttp.onreadystatechange = function(){
    if (xmlHttp.readyState==4 && xmlHttp.status==200)
    {
        document.getElementById('product1').innerHTML = xmlHttp.responseText;
    }
};