通过ajaxSetup(Ember)在模型钩子中设置头文件

时间:2015-08-21 21:05:34

标签: jquery ajax ember.js getjson

我在拨打电话之前尝试设置标头并且它无法正常工作。

model: function() {
        debugger;

        Ember.$.ajaxSetup({
            beforeSend: function(xhr) {
                xhr.setRequestHeader(
                    'Authorization', 'bearer 123456'
                )
        }});

        return Ember.$.getJSON('http://addresss/api/locations').then(function(e) {
            debugger;
            console.log(e);
        });
    }

这不断给我以下错误消息:

  

否'访问控制 - 允许 - 来源'标题出现在请求的上   资源。起源' http://localhost:4200'因此是不允许的   访问。

如果我使用Postman并设置我的标题,它可以正常工作。

1 个答案:

答案 0 :(得分:1)

有时将xhr字段设置为withCredentials - true有助于解决此类错误。请尝试:

Ember.$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.withCredentials = true;
        xhr.setRequestHeader('Authorization', 'bearer 123456');
    }
});
相关问题