如何在Ajax请求中包含头文件?

编程入门 行业动态 更新时间:2024-10-19 14:31:30
本文介绍了如何在Ajax请求中包含头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在YouTube api的ajax调用中包含带有刷新令牌的标头。我正尝试发送删除请求,删除我在我的帐户中的电影。这是我的ajax调用点击按钮点击

I need to include a header with a refresh token in an ajax call to the YouTube api. I am trying to send a delete request, to delete a movie I have on my account. This is my ajax call that fire on button click

jQuery.ajax({ type: 'DELETE', // must set api key url: 'www.googleapis/youtube/v3/videos?id='+ thisUniqueID + '&key=904907387177-qe517sq5dmmpebckjbmrhv4gvac9e2d1.apps.googleusercontent', success: function() { alert('your video has been deleted'); }, error: function() { alert('error processing your requst'); } });

我在收到401(未经授权)的erorr时,似乎需要包含我的访问权限令牌。我正在玩Google api游乐场,看着请求和响应,这就是'Request'被发送出去的情况。

I am receiving a 401 (unauthorized) erorr on return and it seems that I need to include my access token in the call. I was playing around with the google api playground looking at the request and response and this is what shows as the 'Request' being sent out

DELETE www.googleapis/youtube/v3/videos?id=3242343&key={YOUR_API_KEY} Authorization: Bearer "access token" X-JavaScript-User-Agent: Google APIs Explorer

现在根据该请求,它看起来像是头部是与请求一起被发送,其中持有访问令牌。这一定是我得到401错误的原因。如何将这些头文件包含到我的ajax请求中,以便我的访问令牌与请求一起传递?谢谢

Now from that request it looks like the there are headers that are being sent with the request, which hold the access token. This must be why I am getting a 401 error. How can I include those headers into my ajax request, so that my access token is passed along with the request? Thanks

推荐答案

我可以使用下面的代码传递一个头文件:

I was able to pass along a header using this code below:

jQuery.ajax({ type: 'DELETE', // must set api key url: 'www.googleapis/youtube/v3/videos?id='+ thisUniqueID +'&key=api_key_here', beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer access_token_here');}, success: function() { alert('your video has been deleted'); }, error: function() { alert('error processing your request'); } });

更多推荐

如何在Ajax请求中包含头文件?

本文发布于:2023-11-24 21:03:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626814.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:头文件   如何在   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!