ajax如何请求新闻api,ajax请求网络api

转载自:

https://www.cnblogs.com/kudsu/p/9620726.html

啰嗦,直接上代码:

1、在浏览器输入网址:http://api.asilu.com/weather/?callback=getname&city=深圳

你会看到如下结果:他返回的是json数据

/** api.asilu.com **/getname({"city":"深圳","pm25":"28","weather":[{"date":"周一 09月10日","icon1":"day\/duoyun","icon2":"night\/duoyun","weather":"多云","wind":"无持续风向微风","temp":"31 ~ 25℃"},{"date":"周二","icon1":"day\/duoyun","icon2":"night\/duoyun","weather":"多云","wind":"无持续风向微风","temp":"31 ~ 27℃"},{"date":"周三","icon1":"day\/leizhenyu","icon2":"night\/leizhenyu","weather":"雷阵雨","wind":"东风4-5级","temp":"31 ~ 26℃"},{"date":"周四","icon1":"day\/zhenyu","icon2":"night\/zhenyu","weather":"阵雨","wind":"东风3-4级","temp":"30 ~ 26℃"}],"date":"2018-09-10","id":"101280601","t":1536508800});

整理之后是这样的:

48304ba5e6f9fe08f3fa1abda7d326ab.png

/** api.asilu.com **/

getname({

"city": "深圳",

"pm25": "28",

"weather": [{

"date": "周一 09月10日",

"icon1": "day\/duoyun",

"icon2": "night\/duoyun",

"weather": "多云",

"wind": "无持续风向微风",

"temp": "31 ~ 25℃"

}, {

"date": "周二",

"icon1": "day\/duoyun",

"icon2": "night\/duoyun",

"weather": "多云",

"wind": "无持续风向微风",

"temp": "31 ~ 27℃"

}, {

"date": "周三",

"icon1": "day\/leizhenyu",

"icon2": "night\/leizhenyu",

"weather": "雷阵雨",

"wind": "东风4-5级",

"temp": "31 ~ 26℃"

}, {

"date": "周四",

"icon1": "day\/zhenyu",

"icon2": "night\/zhenyu",

"weather": "阵雨",

"wind": "东风3-4级",

"temp": "30 ~ 26℃"

}],

"date": "2018-09-10",

"id": "101280601",

"t": 1536508800

});

48304ba5e6f9fe08f3fa1abda7d326ab.png

2、怎样才能获取上述json的数据呢?很简单:

48304ba5e6f9fe08f3fa1abda7d326ab.png

ajax

天气查询

确定

$(function(){

$('#ajax').on('click',function(){

var $telValue=$('#tel').val();

if($telValue=="") {

alert('不能为空!');

return;

}

$.ajax({

type: 'GET',

dataType:'jsonp',

url: 'http://api.asilu.com/weather/?callback=getname&city='+$telValue,

success: function(data){

var reslutData=data;

console.log(reslutData);

$('#reslut').text("你查询的是:"+reslutData.city+","+"明天的天气是:"+reslutData.weather[0].weather);

}

})

})

})

48304ba5e6f9fe08f3fa1abda7d326ab.png

标签:请求,icon2,api,ajax,weather,night,date,day,icon1

来源: https://www.cnblogs.com/gudaozi/p/10988148.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值