js的跨域一直是个让人头疼的问题
老爸入了个新手机嫌弃自带天气查询不好 功能太多,于是就想用自己写一个简单的天气查询
所以先在pc端写了个demo
因为第一次在js跨域,之前听过js跨域就是坑,嗯,领悟到了。
=====================分割线=====================
废话不说,讲正事。
先贴代码:
btn.onclick=function(){
var place=$("#weather").val();//获取输入框内容
var Url="http://www.sojson.com/open/api/weather/json.shtml?city="+place;////Url前段为天气查询api,度娘给的~ city后面带的字段为uft8的UriEncode码
$.ajax({
url: 'http://query.yahooapis.com/v1/public/yql',
dataType: 'jsonp',
data: {
// q: "select * from json where url=\"http://www.sojson.com/open/api/weather/json.shtml?city=%E5%8C%97%E4%BA%AC\"", //url为要请求的地址
q: "select * from json where url=\"" +Url+"\"",
format: "json"
},
success: function (d) {
// alert(JSON.stringify(d))//远程json数据放在query.results下
// alert(d.query.results.js