为什么AngularJS中的这个http post请求会产生意外的令牌错误?

时间:2016-08-18 02:53:37

标签: angularjs json

我可以使用以下

成功执行HTTP post请求
Configuration Manager

我尝试通过

将其转换为角度HTTP请求
curl --request POST
--url https://api.sendgrid.com/v3/mail/send
--header 'Authorization: Bearer xxxxxxxxxxxxx'
--header 'Content-Type: application/json'
--data '{"personalizations": [{"to": [{"email": "xxx@yahoo.com"}]}],"from": {"email": "xxx@yahoo.com"},"subject": "Hey","content": [{"type": "text/plain", "value": "Test"}]}'

我在 $http({ method: 'POST', url: 'https://api.sendgrid.com/v3/mail/send', headers: { 'Authorization: Bearer xxxxxxxxxxxx' }, data: '{"personalizations": [{"to": [{"email": "xxx@yahoo.com"}]}],"from": {"email": "xxx@yahoo.com"},"subject": "Hey","content": [{"type": "text/plain", "value": "Test"}]}' }). success(function(data, status) {}). error(function(data, status) {}); 行上收到意外的令牌错误。

2 个答案:

答案 0 :(得分:1)

headers对象应该是键/值对象:

headers: {'Authorization': 'Bearer xxxxxx'}

答案 1 :(得分:0)

您忘了在'

之后'Authorization'
headers: {'Authorization': 'Bearer xxxxxx'}

请求:

 $http({method: 'POST',
        url: 'https://api.sendgrid.com/v3/mail/send',
        headers: {
        'Authorization': 'Bearer xxxxxxxxxxxx'
        },
        data: '{"personalizations": [{"to": [{"email": "xxx@yahoo.com"}]}],"from": {"email": "xxx@yahoo.com"},"subject": "Hey","content": [{"type": "text/plain", "value": "Test"}]}'
        }).
        success(function(data, status) {}).
        error(function(data, status) {});