asp.net中ajax调用
想做一个ajax调用asp.net网页,效果是发一个名字过去,返回一个名字+hello的效果。但是现在的情况是可以看到发过去,但html收不到,无法显示出来。下附代码,求...
想做一个ajax调用asp.net网页,效果是发一个名字过去,返回一个名字+hello的效果。但是现在的情况是可以看到发过去,但html收不到,无法显示出来。下附代码,求解决:
html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ajax page</title>
<script type="text/javascript">
function getXMLHttpRequest() {
try {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
return new ActiveXObject("Msxml2.XMLHTTP");
}
}
catch (e) {
return new XMLHttpRequest();
}
}
var myRequest = getXMLHttpRequest();
function responseAjax() {
if (myRequest.readyState == 4) {
if (myRequest.status == 200) {
document.getElementById('div1').innerHTML = myRequest.responseText;
}
}
}
function callAjax() {
var lastname = "John";
var url = "WebForm1.aspx?lastname=" + lastname;
var myRandom = parseInt(Math.random());
myRequest.open("GET", url + "&rand=" + myRandom, true);
myRequest.onreadystatechange = responseAjax();
myRequest.send(null);
}
</script>
</head>
<body>
<div id="div1"></div>
<button onclick="callAjax()">start</button>
</body>
</html>
aspx代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ajax
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string a = Request.QueryString["lastname"];
Response.Write(a + ", Hello!");
Response.End();
}
}
} 展开
html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ajax page</title>
<script type="text/javascript">
function getXMLHttpRequest() {
try {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
return new ActiveXObject("Msxml2.XMLHTTP");
}
}
catch (e) {
return new XMLHttpRequest();
}
}
var myRequest = getXMLHttpRequest();
function responseAjax() {
if (myRequest.readyState == 4) {
if (myRequest.status == 200) {
document.getElementById('div1').innerHTML = myRequest.responseText;
}
}
}
function callAjax() {
var lastname = "John";
var url = "WebForm1.aspx?lastname=" + lastname;
var myRandom = parseInt(Math.random());
myRequest.open("GET", url + "&rand=" + myRandom, true);
myRequest.onreadystatechange = responseAjax();
myRequest.send(null);
}
</script>
</head>
<body>
<div id="div1"></div>
<button onclick="callAjax()">start</button>
</body>
</html>
aspx代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ajax
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string a = Request.QueryString["lastname"];
Response.Write(a + ", Hello!");
Response.End();
}
}
} 展开
1个回答
展开全部
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ajax page</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function callAjax() {
var lastname = "John";
var url = "WebForm1.aspx";
var data = { lastname: lastname };
$.get(url, data, function (e) {
$('#div1').html(e);
});
}
</script>
</head>
<body>
<div id="div1"></div>
<button onclick="callAjax()">start</button>
</body>
</html>
用jquery 吧 如果你一定要用原生的 再告诉我 我给你写
追问
最好是javascript的吧,如果能帮我指出我代码里的错误就更感谢了。
追答
你没有错这地方多了括号
myRequest.onreadystatechange = responseAjax();
改
myRequest.onreadystatechange = responseAjax;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |