JS XMLHTTP object IE browser and non IE browser creation method

Source: Internet
Author: User

JS XMLHTTP object IE browser and non IE browser creation method


Example: Create a XMLHTTP object and request an XML document from the server, which is displayed after the document is returned. The following are examples of IE and non-IE explained

1 IE Browser uses ActiveXObject method to create XMLHTTP objects:

   

var xmlhttpreq = new ActiveXObject ("msxml2.xmlhttp.3.0");

Xmlhttpreq.open ("Get", "Http://localhost/test.xml", false);

Xmlhttpreq.send ();

alert (Xmlhttpreq.responsetext);

2 non-IE browsers use the XMLHttpRequest method to create XMLHTTP objects:

     

var xmlhttpreq = new XMLHttpRequest ();

Xmlhttpreq.open ("Get", "Http://localhost/test.xml", false);

Xmlhttpreq.send ();

alert (Xmlhttpreq.responsetext);

2.2 After the XMLHTTP object is created, because it is a set of APIs, it has a number of methods and properties, such as the Open (), send (), and responsetext used above.

The XMLHTTP object's code is handled in a more fixed way. So the next thing to do is to follow the example in order to understand it.

Let's look at a complete example

JS compatible ff ActiveXObject Create XMLHTTP method
*/

function Createxmlhttpobject ()
{
var xobject = null;
Try
{
Xobject = new ActiveXObject ("msxml2.xmlhttp.4.0");
}
catch (E)
{
Try
{
Xobject = new XMLHttpRequest ();
if (Xobject.overridemimetype) {
Xobject.overridemimetype (' Text/xml ');
}
}
catch (E)
{
Try
{
Xobject = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (E)
{
Try
{
Xobject = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (E)
{
Alert ("error:unable to create XML HTTP object!");
}
}
}
}
return xobject;
}

function Initrequest () {
Request = Createxmlhttpobject ();
}

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.