问题描述
所谓js跨域问题,是指在一个域下的页面中通过js访问另一个不同域下 的数据对象,出于安全性考 虑,几乎所有浏览器都不允许这种跨域访问,这就导致在一些ajax应用中,使用跨域的web service会成为一个问题。可是为什么jQuery getScript 可以跨域呢?是什么原理啊?
解决方案
if ( s.dataType === "script" && type === "GET" && remote ) {var head = document.getElementsByTagName("head")[0] || document.documentElement;var script = document.createElement("script");script.src = s.url;if ( s.scriptCharset ) {script.charset = s.scriptCharset;}// Handle Script loadingif ( !jsonp ) {var done = false;// Attach handlers for all browsersscript.onload = script.onreadystatechange = function() {if ( !done && (!this.readyState ||this.readyState === "loaded" || this.readyState === "complete") ) {done = true;success();complete();// Handle memory leak in IEscript.onload = script.onreadystatechange = null;if ( head && script.parentNode ) {head.removeChild( script );}}};}// Use insertBefore instead of appendChild to circumvent an IE6 bug.// This arises when a base node is used (#2709 and #4378).head.insertBefore( script, head.firstChild );// We handle everything using the script element injectionreturn undefined;}即动态创建script标签http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.htmlhttp://505253293.blog.163.com/blog/static/26899167201282655244720/
解决方案二:
jQuery getScript 通过 GET 方式请求载入并执行一个 JavaScript 文件。相当于通过src的形式的导入一个外部的js