当前位置: 首页 > 图文教程 > 网络编程 > Javascript > js 跨域和ajax 跨域问题小结

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 js 跨域和ajax 跨域问题小结


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 369 ::
收藏到网摘: n/a

大家都知道js是不能跨域的,但我们有时候就要这么用,怎么办呢?办法总是有的. js 要跨域的话,可以用:
<script src="别的网站的js地址(这里的内空可以是动态生成的,如:aaa.jsp、bbb.aspx)都可以。"></script>这样是可以从别的网站里面读取出数据的。
关键是看你灵不灵活,会不会用了。
ajax要跨域的话,可以用服务器去别的网站取内容,如asp.net的:
复制代码 代码如下:

public string GetUrlData(string url)
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.WebResponse webResponse = webRequest.GetResponse();
System.IO.Stream iStream = webResponse.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(iStream, System.Text.Encoding.Default);
string str = sr.ReadToEnd();
sr.Close();
iStream.Close();
webResponse.Close();
return str;
}

这个方法已经写好了,你只要传入网站的url地址,就可以得到你要的ajax返回值。
这样就将别的网站的ajax请求,写到了自已的服务器里,然后你再用ajax去请求自已写好的那个
aspx地址就可以了。
如果是java,也是同样的道理,如:
复制代码 代码如下:

URL url = new URL("http://www.ruanchen.com");
FilterInputStream f=(FilterInputStream)url.openStream();