将标题授权设置为angular $ http post

时间:2016-02-10 08:35:15

标签: javascript angularjs

我想在角度js。

中设置$ http.post请求标题的授权

在简单的html ajax上工作:

droppable

在角度上我收到错误消息 - var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { //do something } }; xhttp.open("POST", url, true); xhttp.setRequestHeader("Authorization", "xxxxxxxxxx"); xhttp.send();

"Cannot read property 'protocol' of undefined"

请帮忙。

4 个答案:

答案 0 :(得分:0)

根据文件

var configHeader = {
  headers: {
    'Authorization': 'xxxxxx'
  }
};

$http.post(url, {}, configHeader).then(function(response) {
  //do something 
});

答案 1 :(得分:0)

$http.post有3个参数:

  • URL
  • 数据
  • config(可选)

您错过了要发布的数据...

答案 2 :(得分:0)

示例角度请求:

var req = {
method: 'POST',
url: 'http://example.com',
headers: {
Content-Type': undefined
},
data: { test: 'test' }
}

我猜,你的configHeader应该是configHeader: {'Authorization': 'xxx'}

答案 3 :(得分:0)

最后我找到了答案!

Chrome中存在一些错误,只有在开发人员工具上使用断点时才会产生此错误。我删除了断点,这个错误再也没有出现在控制台上。

仅供参考 - 这是我在删除断点之前在Chrome控制台上显示的错误:

enter image description here

相关问题