惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Google DeepMind News
Google DeepMind News
S
Schneier on Security
S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
Security Latest
Security Latest
Jina AI
Jina AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recorded Future
Recorded Future
T
Tor Project blog
有赞技术团队
有赞技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News | PayPal Newsroom
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
C
Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
IT之家
IT之家
T
Threatpost
Cyberwarzone
Cyberwarzone
O
OpenAI News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
月光博客
月光博客
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 骨月枫🍁

使用vLLM部署Qwen/Qwen3.5-35B-A3B-FP8并且在DIFY中调用 记录个人数组、字符串自己常忘记的方法,以及ES常用处理方式 解决webpack vue 项目打包生成的文件,资源文件均404问题 js 复制粘贴功能记录 记录下工作中使用的pdf.js 记录下jplayer的简单demo js 函数节流 简要谈谈javascript bind 方法 h5启动原生APP总结 mac上安装mongoDb以及简单使用 mac快捷键整理以及node的基本使用 html5视频video积累 整理下PC和移动获取点击、移动坐标的代码和坑 html5 实现简单的上传 canvas基础学习(四) canvas基础学习(三) canvas基础学习(二) canvas基础学习(一) 移动端使用百度分享代码
用ajax与fetch调用阿里云免费接口
骨月枫🍁 · 2017-09-07 · via 博客园 - 骨月枫🍁

  最近学习态度比较积极,打算用react做一个小个人应用网站...所以从阿里云上买了些免费的接口,什么QQ音乐排行查询接口、IP地址查询、天气预报等等。调用时,发现身份校验可以通过简单修改头部信息的方式,即向头部加入APPCODE的key,以及相应的值。

  但是之前没有用过请求头添加   so 记录学习下...

一、首先直接放demo

1、jQuery

var requestUrl = "http://ali-qqmusic.showapi.com/top?topid=6";
$.ajax({
    type : "get",
    url : requestUrl ,
    dataType : "json" ,
    success: function(data) {
        console.log(data);
    }.bind(this),
    beforeSend: function(xhr) {
        xhr.setRequestHeader("Authorization", "APPCODE .....................");
    }
});

使用ajax的beforeSend方法向头部添加信息

2、fetch

let requestUrl = "http://ali-qqmusic.showapi.com/top?topid=6";
fetch(requestUrl, {
    method: "get",
    headers: {
        Authorization: "APPCODE ......................"
    }
}).then(response => response.json()).then(data => console.log(data)).catch(e => console.log(e));

哈哈哈,其实有点宠fetch,这个时用ES6来写。接下来来整理下相关API,以及简单介绍下fetch

二、记录jQuery ajax 时不时使用,但是快遗忘的API方法

ajax 常用的方法,什么url、type、dataType、success之类的就不提了

1、async  默认是true表示请求为异步请求,如果设置为false表示同步,ajax下面的方法要等请求结束后才会调用

2、beforeSend(xhr)   用于发送请求前修改XMLHttpRequest对象的函数,主要用作修改http头部,就像上面的demo

3、context   用于绑定回调函数的上下文,即this,设置了它就相当于在回调函数上加入了bind(context)一样

4、jsonp    用于跨域操作,重写回调函数的名字

5、timeout    请求超时时间

6、complete(xhr , ts)    无论请求成功or失败的回调函数

三、fetch

之前一直没用用过这种请求方式,现在看来这种请求方式代码显得十分的优美,毕竟Fetch API是基于Promises设计的。fetch是浏览器提供的原生网络请求接口,但是它的兼容性还是有所欠缺的,具体兼容情况如下图

常用的写法有两种

//方式一
fetch('./api/XXX.json')
.then(
    function(response) {
        if(response.status !== 200) {
            console.log('Status Code: ' + response.status);
            return;
        }
        response.json().then(function(data) {
            console.log(data);
        });
    }
)
.catch(function(err) {
    console.log(err);
});
//方式二
function status(response) {  
  if (response.status == 200) {  
    return Promise.resolve(response)  
  } else {  
    return Promise.reject(new Error(response.statusText))  
  }  
}
function json(response) {  
  return response.json()  
}
fetch('./api/XXX.json').then(status).then(json)  
.then(function(data) {  
    console.log(data);
}).catch(function(err) {  
    console.log('err);  
});

  在fetch方法中,首先返回的response对象,so 第一个then中的函数参数为res,然后通过响应的状态码判断请求是否成功,成功后调用response的json()方法,这个也是返回一个Promise对象,所以可以连续then,最后的catch可以抓取代码执行时的异常信息。 

  然后就是fetch第一个参数为URL,第二个参数可以加入请求方式、头信息、表单信息等

fetch(url, {  
    method: 'post',  
    headers: {  
      "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"  
    },  
    body: 'foo=bar&lorem=ipsum'  
})

  最后就是fetch中的一些坑,没遇到过,先记录下。

 1、Fetch 请求默认是不带 cookie 的,需要设置 fetch(url, {credentials: 'include'})

 2、服务器返回 400,500 错误码时并不会 reject,只有网络错误这些导致请求不能完成时,fetch 才会被 reject。

搞定!!!