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 设置标题?


- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01