admin管理员组文章数量:1317915
I want to call a jsp file through ajax post call. So I've done below code -
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
var params = "report_id=0&id=1234567890";
xmlhttp.open("POST","/test/jsp/test.jsp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
</script>
</head>
<body onload="loadXMLDoc()">
<div id="myDiv"></div>
Now test.jsp looks like below -
<html>
<head>
<script language="JavaScript">
function hello()
{
alert("Hello");
//Do my stuff
}
</script>
<title>test Page</title>
</head>
<body topmargin="0" leftmargin="0" onload="hello()">
<form name="mainForm" >
</form>
</body>
</html>
Issue is, I'm not getting alert message when opening my first html page. What is wrong here and what needs to be done?
I want to call a jsp file through ajax post call. So I've done below code -
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
var params = "report_id=0&id=1234567890";
xmlhttp.open("POST","/test/jsp/test.jsp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
</script>
</head>
<body onload="loadXMLDoc()">
<div id="myDiv"></div>
Now test.jsp looks like below -
<html>
<head>
<script language="JavaScript">
function hello()
{
alert("Hello");
//Do my stuff
}
</script>
<title>test Page</title>
</head>
<body topmargin="0" leftmargin="0" onload="hello()">
<form name="mainForm" >
</form>
</body>
</html>
Issue is, I'm not getting alert message when opening my first html page. What is wrong here and what needs to be done?
Share Improve this question asked Oct 4, 2013 at 20:39 PakiraPakira 2,0213 gold badges27 silver badges57 bronze badges 1- possible duplicate of Executing <script> elements inserted with .innerHTML – Quentin Commented Oct 4, 2013 at 20:44
2 Answers
Reset to default 2Instead of trying with onload function, use ready function as
$( document ).ready(function()
{
//here you can call hello function
})
you will not get javascript executed when you are making ajax call like this.
Once ajax call is made you should trigger a function on main page not on ajax page
$.ajax({
type: "POST",
url: "test.jsp",
success: function(){
hello();
},
error: function(){
alert("error");
}
});
function hello()
{
}
本文标签: javascriptHow to call JS function through ajax callStack Overflow
版权声明:本文标题:javascript - How to call JS function through ajax call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742024631a2415310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论