ajax中的赋值问题。

dmy_123 2016-09-03 07:54:04

如图
在变量url中的getcity()方法获取不到当地城市 ,而当单独调用getcity()方法时候,就可以得到当地城市,放在url中而不行了?
...全文
425 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
tckoo 2016-09-08
  • 打赏
  • 举报
回复
引用 15 楼 dmy_123 的回复:
[quote=引用 14 楼 zhuhong639 的回复:] [quote=引用 13 楼 dmy_123 的回复:] [quote=引用 12 楼 zhuhong639 的回复:]
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
	$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
		var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
		$.getJSON(url,function(json){
			$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
			$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
			$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
			$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
			$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
			$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
		});
	});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
朋友 能贴下你截图结果的完整代码吗 我之前也和你的代码一样 但是得不到你图中结果 看完之前的回复 [/quote] 这个就是完整代码了啊,你引用自己的jquery文件,运行就出来了啊。[/quote] 我按照你的代码运行了 结果如图 为什么你的运行可以我却不行
<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
<script>
$(function(){
    $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
        var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
        $.getJSON(url,function(json){
            $('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
            $('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
            $('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
            $('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
            $('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
            $('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
        });
    });
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
[/quote] 你看看'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js'这个请求的结果是什么呢?
dmy_123 2016-09-08
  • 打赏
  • 举报
回复
引用 14 楼 zhuhong639 的回复:
[quote=引用 13 楼 dmy_123 的回复:]
[quote=引用 12 楼 zhuhong639 的回复:]
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
$.getJSON(url,function(json){
$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
});
});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>


朋友 能贴下你截图结果的完整代码吗 我之前也和你的代码一样 但是得不到你图中结果 看完之前的回复
[/quote]
这个就是完整代码了啊,你引用自己的jquery文件,运行就出来了啊。[/quote]
dmy_123 2016-09-08
  • 打赏
  • 举报
回复
引用 14 楼 zhuhong639 的回复:
[quote=引用 13 楼 dmy_123 的回复:]
[quote=引用 12 楼 zhuhong639 的回复:]
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
$.getJSON(url,function(json){
$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
});
});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>


朋友 能贴下你截图结果的完整代码吗 我之前也和你的代码一样 但是得不到你图中结果 看完之前的回复
[/quote]
这个就是完整代码了啊,你引用自己的jquery文件,运行就出来了啊。[/quote]

我按照你的代码运行了 结果如图 为什么你的运行可以我却不行
<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
<script>
$(function(){
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
$.getJSON(url,function(json){
$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
});
});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>


tckoo 2016-09-08
  • 打赏
  • 举报
回复
引用 13 楼 dmy_123 的回复:
[quote=引用 12 楼 zhuhong639 的回复:]
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
	$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
		var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
		$.getJSON(url,function(json){
			$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
			$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
			$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
			$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
			$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
			$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
		});
	});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
朋友 能贴下你截图结果的完整代码吗 我之前也和你的代码一样 但是得不到你图中结果 看完之前的回复 [/quote] 这个就是完整代码了啊,你引用自己的jquery文件,运行就出来了啊。
dmy_123 2016-09-08
  • 打赏
  • 举报
回复
引用 17 楼 zhuhong639 的回复:
[quote=引用 15 楼 dmy_123 的回复:] [quote=引用 14 楼 zhuhong639 的回复:] [quote=引用 13 楼 dmy_123 的回复:] [quote=引用 12 楼 zhuhong639 的回复:]
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
	$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
		var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
		$.getJSON(url,function(json){
			$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
			$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
			$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
			$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
			$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
			$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
		});
	});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
朋友 能贴下你截图结果的完整代码吗 我之前也和你的代码一样 但是得不到你图中结果 看完之前的回复 [/quote] 这个就是完整代码了啊,你引用自己的jquery文件,运行就出来了啊。[/quote] 我按照你的代码运行了 结果如图 为什么你的运行可以我却不行
<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
<script>
$(function(){
    $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
        var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
        $.getJSON(url,function(json){
            $('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
            $('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
            $('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
            $('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
            $('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
            $('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
        });
    });
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
[/quote] 你看看'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js'这个请求的结果是什么呢?[/quote] 原来我chorme版本对跨域比较严格 我设置了允许跨域后就可以了
dmy_123 2016-09-08
  • 打赏
  • 举报
回复
引用 17 楼 zhuhong639 的回复:
[quote=引用 15 楼 dmy_123 的回复:] [quote=引用 14 楼 zhuhong639 的回复:] [quote=引用 13 楼 dmy_123 的回复:] [quote=引用 12 楼 zhuhong639 的回复:]
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
	$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
		var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
		$.getJSON(url,function(json){
			$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
			$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
			$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
			$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
			$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
			$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
		});
	});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
朋友 能贴下你截图结果的完整代码吗 我之前也和你的代码一样 但是得不到你图中结果 看完之前的回复 [/quote] 这个就是完整代码了啊,你引用自己的jquery文件,运行就出来了啊。[/quote] 我按照你的代码运行了 结果如图 为什么你的运行可以我却不行
<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
<script>
$(function(){
    $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
        var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
        $.getJSON(url,function(json){
            $('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
            $('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
            $('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
            $('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
            $('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
            $('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
        });
    });
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
[/quote] 你看看'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js'这个请求的结果是什么呢?[/quote] 原来是浏览器问题 我用火狐可以了 但是用谷歌却不行??
dmy_123 2016-09-08
  • 打赏
  • 举报
回复
引用 17 楼 zhuhong639 的回复:
[quote=引用 15 楼 dmy_123 的回复:]
[quote=引用 14 楼 zhuhong639 的回复:]
[quote=引用 13 楼 dmy_123 的回复:]
[quote=引用 12 楼 zhuhong639 的回复:]
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
$.getJSON(url,function(json){
$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
});
});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>


朋友 能贴下你截图结果的完整代码吗 我之前也和你的代码一样 但是得不到你图中结果 看完之前的回复
[/quote]
这个就是完整代码了啊,你引用自己的jquery文件,运行就出来了啊。[/quote]

我按照你的代码运行了 结果如图 为什么你的运行可以我却不行
<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
<script>
$(function(){
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
$.getJSON(url,function(json){
$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
});
});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>


[/quote]


你看看'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js'这个请求的结果是什么呢?[/quote]
'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js请求和你的一样啊 但是下一个天气请求就不一样了


tckoo 2016-09-07
  • 打赏
  • 举报
回复
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
	$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
		var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
		$.getJSON(url,function(json){
			$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
			$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
			$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
			$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
			$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
			$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
		});
	});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
dmy_123 2016-09-07
  • 打赏
  • 举报
回复
引用 12 楼 zhuhong639 的回复:
<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
	$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){
		var url='https://api.heweather.com/x3/weather?city='+remote_ip_info.city+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
		$.getJSON(url,function(json){
			$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
			$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
			$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
			$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
			$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
			$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
		});
	});
});
</script>
<div id="city"></div>
<div id="data"></div>
<div id="weather"></div>
<div id="airpoint"></div>
<div id="suggest"></div>
<div id="station"></div>
朋友 能贴下你截图结果的完整代码吗 我之前也和你的代码一样 但是得不到你图中结果 看完之前的回复
zhangxinbetter 2016-09-06
  • 打赏
  • 举报
回复
你直接在浏览器中访问这个接口

http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
看看有没有返回值,实在不行你就把 $.getScript 改成同步的 ajax 请求。
引用 9 楼 yuqiz1981 的回复:
改成同步请求就可以了。。
你直接在浏览器中访问这个接口

http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
看看有没有返回值,实在不行你就把 $.getScript 改成同步的 ajax 请求,这个接口返回的本来就是 json,用$.getScript 干什么。
dmy_123 2016-09-05
  • 打赏
  • 举报
回复
引用 7 楼 david_csu 的回复:
[quote=引用 6 楼 dmy_123 的回复:]
[quote=引用 1 楼 david_csu 的回复:]
getCity() 方法中的 $.getScript 是异步加载。所以在 $.getJSON 使用 url 的时候,$.getScript 还在请求接口,即getCity方法还没执行完,所以 url 中没有city的值。

$(document).ready(function() {   
var strCity;
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(){
strCity=remote_ip_info.city;
var url='https://api.heweather.com/x3/weather?city='+strCity+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
$.getJSON(url,function(json){
$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
});
});
});

像这样 错在哪里?getjson中还是得不到url的数据[/quote]

为什么我测的就能打印呢?

$(document).ready(function() {
var strCity;
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(){
strCity=remote_ip_info.city;
var url='https://api.heweather.com/x3/weather?city='+strCity+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
console.log(url);
$.getJSON(url,function(json){
console.log(json);
});
});
});


[/quote]
你看一下下面的值 没有获取到的城市 unknown city
kadxls 2016-09-05
  • 打赏
  • 举报
回复
这是在拼网速快慢。。
zhangxinbetter 2016-09-05
  • 打赏
  • 举报
回复
引用 6 楼 dmy_123 的回复:
[quote=引用 1 楼 david_csu 的回复:]
getCity() 方法中的 $.getScript 是异步加载。所以在 $.getJSON 使用 url 的时候,$.getScript 还在请求接口,即getCity方法还没执行完,所以 url 中没有city的值。

$(document).ready(function() {   
var strCity;
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(){
strCity=remote_ip_info.city;
var url='https://api.heweather.com/x3/weather?city='+strCity+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
$.getJSON(url,function(json){
$('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
$('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
$('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
});
});
});

像这样 错在哪里?getjson中还是得不到url的数据[/quote]

为什么我测的就能打印呢?

$(document).ready(function() {
var strCity;
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(){
strCity=remote_ip_info.city;
var url='https://api.heweather.com/x3/weather?city='+strCity+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
console.log(url);
$.getJSON(url,function(json){
console.log(json);
});
});
});


yuqiz1981 2016-09-05
  • 打赏
  • 举报
回复
改成同步请求就可以了。。
dmy_123 2016-09-04
  • 打赏
  • 举报
回复
引用 4 楼 jslang 的回复:
[quote=引用 3 楼 dmy_123 的回复:] [quote=引用 2 楼 jslang 的回复:] ajaxj 是异步加载数据的。 异步模式是不能用return语句返回的,必须要改用回调函数。 在ajax请求加载数据的这行代码的后面不要写任何需要用到加载的数据的代码。因为这些代码执行时数据肯定还没有加载的。 所有需要用到加载的数据的代码都必须放在数据加载完成的回调函数中才能正解获取数据。
请问如果那么把getjson()放在getscript()的回调函数中是否可以用到getscript()加载的数据?[/quote] 放在getscript()的回调函数中就可以[/quote]
$(document).ready(function() {	
		var strCity;
		$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(){
		strCity=remote_ip_info.city;	
	 var url='https://api.heweather.com/x3/weather?city='+strCity+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
		$.getJSON(url,function(json){
	    $('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
		$('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
		$('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
		$('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
        $('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
        $('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
	 });	
	});
	});
像这样getjson还是得不到url的数据
dmy_123 2016-09-04
  • 打赏
  • 举报
回复
引用 1 楼 david_csu 的回复:
getCity() 方法中的 $.getScript 是异步加载。所以在 $.getJSON 使用 url 的时候,$.getScript 还在请求接口,即getCity方法还没执行完,所以 url 中没有city的值。
$(document).ready(function() {   
        var strCity;
        $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(){
        strCity=remote_ip_info.city;   
     var url='https://api.heweather.com/x3/weather?city='+strCity+'&key=cc6b92d4bbd74ab5951b2a41bd10b7a3';
        $.getJSON(url,function(json){
        $('#city').html(json["HeWeather data service 3.0"][0].basic.city);//城市天气
        $('#data').html(json["HeWeather data service 3.0"][0].basic.update.loc);//更新时间
        $('#weather').html(json["HeWeather data service 3.0"][0].now.tmp);//当前温度
        $('#airpoint').html(json["HeWeather data service 3.0"][0].aqi.city.aqi);//空气质量指数
        $('#suggest').html(json["HeWeather data service 3.0"][0].suggestion.comf.txt);//天气简介
        $('#station').html(json["HeWeather data service 3.0"][0].now.cond.txt);
     });   
    });
    });
像这样 错在哪里?getjson中还是得不到url的数据
天际的海浪 2016-09-03
  • 打赏
  • 举报
回复
引用 3 楼 dmy_123 的回复:
[quote=引用 2 楼 jslang 的回复:] ajaxj 是异步加载数据的。 异步模式是不能用return语句返回的,必须要改用回调函数。 在ajax请求加载数据的这行代码的后面不要写任何需要用到加载的数据的代码。因为这些代码执行时数据肯定还没有加载的。 所有需要用到加载的数据的代码都必须放在数据加载完成的回调函数中才能正解获取数据。
请问如果那么把getjson()放在getscript()的回调函数中是否可以用到getscript()加载的数据?[/quote] 放在getscript()的回调函数中就可以
dmy_123 2016-09-03
  • 打赏
  • 举报
回复
引用 2 楼 jslang 的回复:
ajaxj 是异步加载数据的。 异步模式是不能用return语句返回的,必须要改用回调函数。 在ajax请求加载数据的这行代码的后面不要写任何需要用到加载的数据的代码。因为这些代码执行时数据肯定还没有加载的。 所有需要用到加载的数据的代码都必须放在数据加载完成的回调函数中才能正解获取数据。
请问如果那么把getjson()放在getscript()的回调函数中是否可以用到getscript()加载的数据?
天际的海浪 2016-09-03
  • 打赏
  • 举报
回复
ajaxj 是异步加载数据的。 异步模式是不能用return语句返回的,必须要改用回调函数。 在ajax请求加载数据的这行代码的后面不要写任何需要用到加载的数据的代码。因为这些代码执行时数据肯定还没有加载的。 所有需要用到加载的数据的代码都必须放在数据加载完成的回调函数中才能正解获取数据。
zhangxinbetter 2016-09-03
  • 打赏
  • 举报
回复
getCity() 方法中的 $.getScript 是异步加载。所以在 $.getJSON 使用 url 的时候,$.getScript 还在请求接口,即getCity方法还没执行完,所以 url 中没有city的值。

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧