Asp. NET tip: Cannot create XMLHTTP control

Source: Internet
Author: User
Tags functions
asp.net|xml| Create | tips | controls

Recently, using AJAX to develop server programs, it was found that IE browsers do not support XMLHttpRequest objects and that microsoft.xmlhttp controls are not found.

The problem arises and we need to solve the solution as follows:

1, under the operation of Regsvr32 msxml3.dll;
2, with ready-made frame to do Ajax;
3, code optimization:

if (window. ActiveXObject)
{
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (window. XMLHttpRequest)
{
XmlHttp = new XMLHttpRequest ();
}

if (handle_s = null)
handle_s = "bin/normal.py/db";
This.xmlHttp.onreadystatechange = handle_l;
This.xmlHttp.open ("Get", handle_s,true);
This.xmlHttp.send (NULL);

or judge the browser

var agt = Navigator.userAgent.toLowerCase ();
var Is_ie = (Agt.indexof ("MSIE")!=-1);
var is_ie5 = (Agt.indexof ("MSIE 5")!=-1);
var Is_opera = (Agt.indexof ("opera")!=-1);
var Is_mac = (Agt.indexof ("Mac")!=-1);
var Is_gecko = (Agt.indexof ("Gecko")!=-1);
var Is_safari = (Agt.indexof ("Safari")!=-1);

function Createxmlhttpreq (handler) {

var xmlhttp = null;
if (Is_ie) {
Guaranteed to is IE5 or IE6
var control = (IS_IE5)? "Microsoft.XMLHTTP": "Msxml2.xmlhttp";

try {
XMLHTTP = new ActiveXObject (control);
Xmlhttp.onreadystatechange = handler;
catch (ex) {
Todo:better Help Message
Alert ("You are need to enable Active Scripting and ActiveX controls");
}

} else {

Mozilla
XMLHTTP = new XMLHttpRequest ();
Xmlhttp.onload = handler;
Xmlhttp.onerror = handler;

}

return XMLHTTP;
}

Or

<script language= "JavaScript" >
var http_request = false;
function send_request (URL) {//initialization, specifying handler functions, sending requests
Http_request = false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest) {//mozilla browser
Http_request = new XMLHttpRequest ();
if (http_request.overridemimetype) {//Set MIME category
Http_request.overridemimetype (' Text/xml ');
}
}
else if (window. ActiveXObject) {//IE browser
try {
Http_request = new ActiveXObject ("Msxml2.xmlhttp");
catch (e) {
try {
Http_request = new ActiveXObject ("Microsoft.XMLHTTP");
catch (e) {}
}
}
if (!http_request) {//exception, failed to create object instance
Window.alert ("Cannot create XMLHttpRequest object instance.");
return false;
}
Http_request.onreadystatechange = ProcessRequest;
Determine how and when the request is sent and whether the next code is executed synchronously
Http_request.open ("Get", url, True);
Http_request.send (NULL);
}
Functions that process return information
function ProcessRequest () {
if (http_request.readystate = = 4) {//Judge object state
if (Http_request.status = = 200) {//information has been successfully returned to start processing information
var returnobj = Http_request.responsexml;
var xmlobj = Http_request.responsexml;
var employees = Xmlobj.getelementsbytagname ("employee");
var feedbackstr = "";
for (Var i=0;i<employees.length;i++) {//loop read employees.xml content
var employee = Employees[i];
Feedbackstr + = "Employee:" + employee.getattribute ("name");/get label Specify attribute
Feedbackstr + = "Position:" + employee.getelementsbytagname ("job") [0].firstchild.data;//gets the initial data of the specified label
Feedbackstr + = "Wages:" + employee.getelementsbytagname ("salary") [0].firstchild.data;
Feedbackstr + = "\ r \ n";
}
alert (FEEDBACKSTR);
} else {//page is not normal
Alert ("The page you are requesting has an exception.) ");
}
}
}
</script>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.