How to solve cross-browser AJAX Problems

Source: Internet
Author: User

I. Solve the Problem of cross-origin access failure in firefox

In essence, AJAX uses the XMLHttpRequest component to send HTTP requests to the server. Please receive the relevant information. The operation after the response information is successfully received is similar to that of a common Web Client Program (DOM is generally used to add the information to the HTML component ). But the problem occurs on the XMLHttpRequest component. This name is used in most browsers (including IE and Firefox. The usage is similar. However, some operations have different effects.

Take cross-origin access as an example. Let's take a look at the following javascript code in html:

Test.html


<Html>
<Head>
<Title> </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script type = "text/javascript">

// Obtain the XMLHttpRequest object in IE and firefox browsers
Function getXMLHTTPRequest ()
{
Var myRequest = null;
If (window. XMLHttpRequest) // firefox
{
MyRequest = new XMLHttpRequest ();
}
Else if (typeof ActiveXObject! = "Undefined") // IE
{
MyRequest = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Return myRequest;
}

Var myRequest;
Function onReadyState () // XMLHttpRequest processes events in asynchronous access status
{
If (myRequest. readyState = 4) // 4 indicates that the corresponding information is obtained successfully.
{
Var msg = document. getElementById ("msg ");
Msg. value = myRequest. responseText
}
}
Function getServiceText ()
{
MyRequest = getXMLHTTPRequest ();
If (myRequest)
{
MyRequest. onreadystatechange = onReadyState;
Try
{
MyRequest. open ("post", "http://www.blogjava.net", true );
}
Catch (exception)
{
Var msg = document. getElementById ("msg ");
Msg. value = exception;
}
MyRequest. send ("test ");
}
}

</Script>
</Head>
<Body>
<Input id = "msg" type = "text"/>
<Input type = "button" value = "information" onclick = "getServiceText ()"/>
</Body>
</Html>

If you access the preceding html file in IE, the url is as follows:

Http: /localhost: 8080/test.html

A dialog box will pop up, which probably means that you have accessed the domain, which may be risky and whether to continue. If you continue, you can still access the http://www.blogjava.net. However, the url of other domains cannot be accessed in firefox, and an exception is thrown, "the permission is insufficient when the XMLHttpRequest. open method is called. There are two solutions: Modify firefox settings, enter "about: config" in the address bar of firefox, and find signed. applets. set codebase_principal_support to true. 1.

  

Figure 1

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.