Set Headers with jQuery.ajax and JSONP?(使用 jQuery.ajax 和 JSONP 设置标题?)
问题描述
我正在尝试使用 jQuery 访问谷歌文档.到目前为止,这是我所拥有的:
I am trying to access google docs with jQuery. Here's what I have so far:
var token = "my-auth-token";
$.ajax({
url: "http://docs.google.com/feeds/documents/private/full?max-results=1&alt=json",
dataType: 'jsonp',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "GoogleLogin auth=" + token);
},
success: function(data, textStatus, XMLHttpRequest) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
如果我将 dataType
设置为 jsonp
(来自 使用 jQuery 进行跨域 Ajax 请求).如果我遗漏了 jsonp
,我将无法发出跨域请求.如果我使用 jQuery.getJSON
,我不能传入任何标题...
It doesn't allow me to set headers if I set the dataType
to jsonp
(from Make Cross Domain Ajax Requests with jQuery). If I leave out jsonp
, I can't make the cross-domain request. If I use jQuery.getJSON
, I can't pass in any headers...
在发出跨域 ajax 请求(在 jQuery 中)时,有什么方法可以定义自定义标头?
Is there any way to define custom headers when making a cross-domain ajax request (in jQuery)?
推荐答案
这是不可能的.
JSONP 请求通过创建 <script>
元素并将其 src
属性设置为请求 URL 来工作.
您不能向 <script>
元素发送的 HTTP 请求添加自定义标头.
A JSONP request works by creating a <script>
element with its src
attribute set to the request URL.
You cannot add custom headers to the HTTP request sent by a <script>
element.
这篇关于使用 jQuery.ajax 和 JSONP 设置标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 jQuery.ajax 和 JSONP 设置标题?


基础教程推荐
- 在 contenteditable 中精确拖放 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01