本文介绍了早点了解AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正试着早日考虑AJAX。 我无法获得成功并运行open和send命令。 < script type = text / javascript> var xhr; if ( window .XMLHttpRequest){ xhr = new XMLHttpRequest(); } 其他 如果( window .ActiveXObject){ xhr = new ActiveXObject( MSXML2.XMLHTTP); document .getElementById( <% = Label1.ClientID%>)= Windows; } 其他 { throw new 错误( 此浏览器不支持Ajax); document .getElementById( <% = Label2.ClientID%>)= 此浏览器不支持Ajax; } xhr.onreadystatechange = function (){ if (xhr.readyState == 4 ){ if (xhr.status> = 200 && xhr.status< 300 ){ // 成功执行 document .getElementById( <%= Label3.ClientID%>)= 成功; xhr.open( POST,Default.aspx / GetValue); xhr.send(); } 其他 { // 在错误时执行 document .getElementById( <%= Label2.ClientID%>)= 错误; } } } 解决方案 这就是为什么:你只是创建Ajax对象,而不是发送任何请求。然后你将一个事件处理程序添加到 xhr 并期望它改变状态,但状态不会改变,因为你没有对这个对象做任何其他事情。 要获得一些效果,您需要在添加事件处理程序后发送HTTP请求。 请在此处找到最小代码示例:http://www.w3.org/TR/XMLHttpRequest/#introduction [ ^ ]。 另外,你做的很糟糕:使用 ActiveXObject 。这是过时的不安全的东西。如果一些精通安全的用户了解到您正在使用这种肮脏的技巧,那么他们将您的网站列入黑名单将是一个很好的理由。如果有人使用这个过时的浏览器(与IE6和早期版本一样旧)该怎么办?简单:不做任何事情并向用户显示警告。请参阅: http://www.ie6nomore.com [ ^ ]。 -SA I am trying to get my head around early AJAX. I can not get "Success" and run the open and send commands.<script type="text/javascript"> var xhr; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Msxml2.XMLHTTP"); document.getElementById("<%=Label1.ClientID%>") = "Windows"; } else { throw new Error("Ajax is not supported by this browser"); document.getElementById("<%=Label2.ClientID%>") = "Ajax is not supported by this browser"; } xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if (xhr.status >= 200 && xhr.status < 300) { //executes on success document.getElementById("<%=Label3.ClientID%>") = "Success"; xhr.open("POST", Default.aspx / GetValue); xhr.send(); } else { //executes on error document.getElementById("<%=Label2.ClientID%>") = "Error"; } } } 解决方案 This is why: you just create Ajax object, not sending any request. And then you add an event handler to xhr and expect it to change state, but the state is not changed because you don't do anything else to this object.To get some effect, you need to send HTTP request after adding the event handler.Please find the minimal code sample here: http://www.w3.org/TR/XMLHttpRequest/#introduction[^]. Also, you are doing a very bad thing: using ActiveXObject. This is obsolete inherently unsafe things. If some security-savvy users learn that you are using this dirty trick, it will be a good reason for them to blacklist your site. What to do if some use this obsolete browser (as old as IE6 and earlier versions)? Simple: don't do anything and show the user a warning. Please see:http://www.ie6nomore.com[^].—SA 这篇关于早点了解AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 22:09