The error message "Access-Control-Allow-Origin" is missing in the CORS header when remote js is dynamically loaded.

Source: Internet
Author: User

Example

AjaxPage ("yy", "http://static.111cn.net/js/jquery-1.8.0.min.js ");

Function ajaxPage (sId, url)
{
Var oXmlHttp = getHttpRequest ();
OXmlHttp. onreadystatechange = function ()
 {
// 4 indicates that the data has been sent
If (oXmlHttp. readyState = 4)
  {
// 0 indicates the local access, 200 indicates that the access to the server is successful, and 304 indicates that the access to the cache is not modified.
If (oXmlHttp. status = 200 | oXmlHttp. status = 0 | oXmlHttp. status = 304)
   {
IncludeJS (sId, oXmlHttp. responseText );
   }   
  }
 }
OXmlHttp. open ("GET", url, false); // true indicates asynchronous loading (that is, other code can be run while sending ajax) false: // synchronous loading (other code cannot be executed when loading );
OXmlHttp. send (null );
}

Function getHttpRequest ()
{
If (window. ActiveXObject) // IE
 {
Return new ActiveXObject ("MsXml2.XmlHttp ");
 }
Else if (window. XMLHttpRequest) // Others
 {
Return new XMLHttpRequest ();
 }
}

Result error

Cross-source requests intercepted: same-origin policy prohibits reading remote resources that are located in the http://static.111cn.net/js/jquery-1.8.0.min.js. (Cause: the CORS header lacks 'access-Control-Allow-origin ').


Cause analysis

The error report above shows that cross-source requests cannot be implemented through ajax. Therefore, you can directly call the content in the current domain or directly use jsinclude.

Solution

AjaxPage ("yy", "http://static.111cn.net/js/jquery-1.8.0.min.js ");

Modify

AjaxPage ("yy", "/js/jquery-1.8.0.min.js");

Of course, if you can modify the page's direct loading <script language = "javascript" src = "js/v4/jquery. js"> </script> method, there will be no problem.

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.