使用 jQuery.ajax 和 JSONP 设置标题? [英] Set Headers with jQuery.ajax and JSONP?

查看:26
本文介绍了使用 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 设置标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆