分享:使用JQuery进行跨域请求
Ajax不能跨域请求???试试下面的吧:
注:如果网速较慢,那请耐心等待 js 文件加载完成再测试,否则看不到效果!(非 iframe 实现!)
以上程序是今天偶然看到的,分享一下!
当然,还有以上的那个 Demo,我直接拿过来改了下,原地址:Demo

1 function doAjax(url,msg,container){
2 // if the URL starts with http
3 if(url.match('^http')){
4 // assemble the YQL call
5 msg.removeClass('error');
6 msg.html(' (loading...)');
7 $.getJSON("http://query.yahooapis.com/v1/public/yql?"+
8 "q=select%20*%20from%20html%20where%20url%3D%22"+
9 encodeURIComponent(url)+
10 "%22&format=xml'&callback=?",
11 function(data){
12 if(data.results[0]){
13 var data = filterData(data.results[0]);
14 msg.html(' (ready.)');
15 container.
16 html(data).
17 focus().
18 effect("highlight",{},1000);
19 } else {
20 msg.html(' (error!)');
21 msg.addClass('error');
22 var errormsg = '<p>Error: could not load the page.</p>';
23 container.
24 html(errormsg).
25 focus().
26 effect('highlight',{color:'#c00'},1000);
27 }
28 }
29 );
30 } else {
31 $.ajax({
32 url: url,
33 timeout:5000,
34 success: function(data){
35 msg.html(' (ready.)');
36 container.
37 html(data).
38 focus().
39 effect("highlight",{},1000);
40 },
41 error: function(req,error){
42 msg.html(' (error!)');
43 msg.addClass('error');
44 if(error === 'error'){error = req.statusText;}
45 var errormsg = 'There was a communication error: '+error;
46 container.
47 html(errormsg).
48 focus().
49 effect('highlight',{color:'#c00'},1000);
50 },
51 beforeSend: function(data){
52 msg.removeClass('error');
53 msg.html(' (loading...)');
54 }
55 });
56 }
57 }
2 // if the URL starts with http
3 if(url.match('^http')){
4 // assemble the YQL call
5 msg.removeClass('error');
6 msg.html(' (loading...)');
7 $.getJSON("http://query.yahooapis.com/v1/public/yql?"+
8 "q=select%20*%20from%20html%20where%20url%3D%22"+
9 encodeURIComponent(url)+
10 "%22&format=xml'&callback=?",
11 function(data){
12 if(data.results[0]){
13 var data = filterData(data.results[0]);
14 msg.html(' (ready.)');
15 container.
16 html(data).
17 focus().
18 effect("highlight",{},1000);
19 } else {
20 msg.html(' (error!)');
21 msg.addClass('error');
22 var errormsg = '<p>Error: could not load the page.</p>';
23 container.
24 html(errormsg).
25 focus().
26 effect('highlight',{color:'#c00'},1000);
27 }
28 }
29 );
30 } else {
31 $.ajax({
32 url: url,
33 timeout:5000,
34 success: function(data){
35 msg.html(' (ready.)');
36 container.
37 html(data).
38 focus().
39 effect("highlight",{},1000);
40 },
41 error: function(req,error){
42 msg.html(' (error!)');
43 msg.addClass('error');
44 if(error === 'error'){error = req.statusText;}
45 var errormsg = 'There was a communication error: '+error;
46 container.
47 html(errormsg).
48 focus().
49 effect('highlight',{color:'#c00'},1000);
50 },
51 beforeSend: function(data){
52 msg.removeClass('error');
53 msg.html(' (loading...)');
54 }
55 });
56 }
57 }
看看上面的脚本,发现什么?YQL?有兴趣的朋友可以了解下!
看来文章有些“标题党”,完成跨域请求的并不是Jquery,而是YQL!
既然可以实现跨域请求,你会想到什么呢?欢迎大家一起探讨!
细想一下:利用WebService,自己也可以实现...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?