[英]How to call JS function through ajax call
我想通過ajax調用來調用jsp文件。 所以我完成了以下代碼-
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>
現在test.jsp如下所示-
<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>
問題是,打開我的第一個HTML頁面時,我沒有收到警告消息。 這里有什么問題,需要做什么?
與其嘗試使用onload函數,不如使用ready函數作為
$( document ).ready(function()
{
//here you can call hello function
})
當您進行這樣的ajax調用時,您將不會執行JavaScript。
一旦進行了ajax調用,您應該在主頁上而不是ajax頁面上觸發一個函數
$.ajax({
type: "POST",
url: "test.jsp",
success: function(){
hello();
},
error: function(){
alert("error");
}
});
function hello()
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.