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