不能设置在$阿贾克斯自定义页眉 [英] Can't set custom headers in $.ajax

查看:124
本文介绍了不能设置在$阿贾克斯自定义页眉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定尝试使用jQuery一次,因为它已经包含在页面中。我发现,它具有非常方便的 $。阿贾克斯的方法,可以让你产生可读的Ajax调用。

I decided to try using jQuery for once because it was already included in the page. I found out that it has very convenient $.ajax method that allows you to produce readable Ajax calls.

我学会了如何添加自定义页眉来电话,因为我需要授权的Imgur。这是我的code:

I learned how to add custom headers to the call, since I need to authorize at Imgur. This is my code:

  $.ajax({
    url: 'https://api.imgur.com/3/image',
    type: 'POST',
    headers: {
      Authorization: 'Client-ID ' + APP_ID,
      Accept: 'application/json'
    },
    data: {
      image: SavePage.getimgrc(),
      type: 'base64'
    },
    success: function(result) {
      var id = result.data.id;
      window.location = 'https://imgur.com/gallery/' + id;
    }
  });

好了,不管它是多么的方便,它不为标榜的工作:

Well, no matter how convenient it is, it does not work as advertised:

POST /3/image HTTP/1.1
Host: api.imgur.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 20956
Origin: resource://jid0-gxjllfbcoax0lcltedfrekqdqpi-at-jetpack
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

那么,是不是坏了还是我搞砸了?

So, is it broken or did I screw it up?

推荐答案

所以,我终于想通了这一点。问题是,尽管我的previous经验,我使用jQuery。本机code正常工作:

So I have finally figured this out. The problem was, that despite my previous experiences, I was using jQuery. The native code works normally:

  var r = new XMLHttpRequest();
  r.open("POST", 'https://api.imgur.com/3/image');
  r.setRequestHeader("Authorization", 'Client-ID ' + APP_ID);
  r.setRequestHeader("Accept", 'application/json ');

  var data = new FormData();
  data.append("type", "base64");
  data.append("image", SavePage.getimgrc());

  r.send(data);

教训:该jQuery是包含在现场的事实并不意味着它会帮助你。

Lesson learned: The fact that the jQuery is included on the site doesn't mean it will help you.

这篇关于不能设置在$阿贾克斯自定义页眉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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