前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jquery利用雅虎YQL 做中间层进行跨域请求

jquery利用雅虎YQL 做中间层进行跨域请求

作者头像
全栈若城
发布2024-02-29 18:15:57
540
发布2024-02-29 18:15:57
举报
文章被收录于专栏:若城技术专栏若城技术专栏

废话不多说直接上代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<style>
		ul li{
			list-style: none;
		}
	</style>
	<body>
		<button>解梦</button> 
		<input type="text" />
		<ul>
			
		</ul>
	</body>
	<script type="text/javascript" src="js/jquery-2.1.0.js" ></script>
	<script>
		$("button").on("click",function(){
			var dream=$("input").val()
//在这个我们定义一下需要请求的地址
			var target="http://v.juhe.cn/dream/query?key=8194d647c67f88eedd63c&q="+dream
		$.ajax({
			type:"get",
//这里请求的是雅虎的yql地址
			url: 'http://query.yahooapis.com/v1/public/yql',
			async:true,
			dataType:"jsonp",
// 雅虎代理要求的数据格式
			 data: {    
                q: "select * from json where url=\'" + target+ "'",    
                //代理返回格式
                format: "json"    
           },    
			success:function(res){
				console.log(res)
				var theword=res.query.results.json.result
				console.log(theword)
				for (var i=0 ; i<theword.length ; i++) {
					if(theword[i].des==undefined){
						theword[i].des="预示为空"
					}else{
					$("ul").append("<li>梦见:"+theword[i].title+"</li><li>预示:"+theword[i].des+"</li>")
						
					}
					

					
				}
			}
			
		});
		})
		
		
	
	</script>
</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-02-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档