Access web resources through Ajax cross-origin in Firefox
I. SolutionFirefoxCross-origin access failure in
AjaxIn essence, naming is used.XMLHttpRequestComponent to send to the serverHTTPRequest, please receive the corresponding information. As for the operations after the response information is successfully receivedWebClientProgramSimilarDomAdd informationHtmlComponent ). But the problem occurs.XMLHttpRequestComponent. Although in most browsers (includingIE,FirefoxAnd so on. The usage is similar. However, some operations have different effects.
Take cross-origin access as an example. Let's take a look at the followingHtmlInJavascriptCode:
Test.html
< Html >
< Head >
< Title > </ Title >
< Meta HTTP-equiv = "Content-Type" Content = "Text/html; charsets = UTF-8" >
< Script Type = "Text/JavaScript" >
// Obtain the XMLHTTPRequest object in IE and Firefox
Function Getxmlhttprequest ()
{
VaR Myrequest = Null ;
If (Window. XMLHttpRequest) // Firefox
{
Myrequest = New XMLHttpRequest ();
}
Else If ( Typeof Activexobject ! = " # Ff0000 " ) // 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 = "Info" Onclick = "Getservicetext ()" />
</ Body >
</ Html >
IfIEAccessHtmlFile,URLAs 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 the execution continues, you can still accessHttp://www.blogjava.net. HoweverFirefoxBut cannot access other domainsURLAnd will throw the "Call MethodXMLHttpRequest. OpenThe permission is insufficient. "exception. There are two solutions:FirefoxInFirefoxEnter"About: configAnd findSigned. Applets. codebase_principal_support, Set itTrue.1.
Figure 1
However, after implementation, you still cannot accessURLI wonder if it's a new version.FirefoxBlock this. Even if this method is feasible, try not to use this configuration.FirefoxTo use the programming method, you can use the following code to open the permission before accessing other domains:
Try
{
Netscape. Security. privilegemanager. enableprivilege ( " Universalbrowserread " );
}
Catch (Exception)
{
Alert (exception );
}
Note that the above Code can only be used inFirefoxSo put it in onlyFirefoxCode block to be executed, as shown in the following section:
If (Window. XMLHttpRequest) // Firefox
{
Try
{
Netscape. Security. privilegemanager. enableprivilege ( " Universalbrowserread " );
}
Catch (Exception)
{
Alert (exception );
}
}
You cannot place the above CodeGetxmlhttprequest, Should be placed inGetservicetext.
NOTE: IfTest.htmlInWebWhen accessing a local fileTest.htmlInIEThe above dialog box does not appear, but you can directly access other domains.
2. Other cross-browser Problems
XMLHttpRequest for different browsers although the interfaces are the same, however, the method and attribute calling XMLHttpRequest in different browsers have different effects. For example, in the send method, in ie , as shown in myrequest. send (); it still works normally, while in firefox , a parameter must be passed for the send method, that is, in firefox , send the method parameter has no default value and must be assigned a value. For general purpose, we recommend that you assign a parameter value to all send methods, even if they are empty strings.
BesidesSendMethod,ResponsetextThe same is true for attributes.IEMedium,ResponsetextReturns the entireWebResource content, whileFirefoxReturns onlyWebThe first line of the resource.
Posted on Galaxy messenger reading (648) Comments (7) EDIT favorite categories: Ajax, web, JavaScript