ajax不调用后台代码
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="addType()" />
<script type="text/javascript" >
function addType() {
var getType = prompt("Please input Type", '');
if (getType!=null) {
var XmlHttp = createXMLHttpResponse();
if (XmlHttp) {
XmlHttp.onreadystatechange = interactWithServer(XmlHttp);
XmlHttp.open("GET", " interactWithAJAX.aspx?action=addType&type=" + getType,true);
XmlHttp.send();
}
else {
alert("XMLHttpResponse is null");
}
}
else {
alert("Type not be save");
}
}
function createXMLHttpResponse (){
if(window.ActiveXObject )
return new ActiveXObject("Microsoft.XMLHTTP");
else(window.XMLHttpRequest)
return new XMLHttpRequest();
}
function interactWithServer(XmlHttp) {
if (XmlHttp.readyState == 4) {
if (XmlHttp.status == 200) {
var isAddToDB = XMLHttpRequest.responseText;
alert(isAddToDB);
}
else
{
alert("There are some problem on server ,and it's status is 400");
}
}
}
</script>
1我第一次写ajax,但是它不去调用asp.net的后台c#代码。请大家帮帮忙,看看为什么interactWithServer(XmlHttp)不去调用后台代码呢?
2 我有些不理解 :
XmlHttp.onreadystatechange = interactWithServer(XmlHttp); //1
XmlHttp.open("GET", " interactWithAJAX.aspx?action=addType&type=" + getType,true); //2
XmlHttp.send();//3
1 不是已经连接上了服务器了吗?为什么2 open 和3 send 还在后面呢?
求解 Ajax JavaScript
[解决办法]
XmlHttp.onreadystatechange = interactWithServer(XmlHttp);
这行代码有问题,返回值永远为0,
注意URL是否需要加\