传递自定义页眉到Ajax请求上选择二 [英] Passing Custom Headers to Ajax request on Select2

查看:161
本文介绍了传递自定义页眉到Ajax请求上选择二的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在努力实现选择二阿贾克斯远程数据加载: -

We are trying to implement Ajax Remote data loading in Select2:-

 $scope.configPartSelect2 =  {
        minimumInputLength: 3,
        ajax: {
            url: "/api/Part",
           // beforeSend: function (xhr) { xhr.setRequestHeader('Authorization-Token', http.defaults.headers.common['Authorization-Token']); },
          //  headers: {'Authorization-Token': http.defaults.headers.common['Authorization-Token']},
            data: function (term, page) {
                return {isStockable: true};
            },
            results: function (data, page) {
                // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                  return { results: data };

            }
        }
    };

我们正在使用AngularJS。
随着每个HTTP请求,我们设置它的默认有我们的authToken作为标题。
但不知何故,它不与选择二Ajax请求协同工作。在上述code,评论code是我失败的尝试。

We are using AngularJS. With each Http request we have set it's default to have our Authtoken as header. But somehow it is not working in conjunction with Select2 Ajax request. In above code, commented code are my failed attempts.

推荐答案

这SELECT2的演示页摘自:

Taken from select2's demo page:

选择二将通过的任何选项中的AJAX对象jQuery的$阿贾克斯的功能,或您指定的运输功能。

Select2 will pass any options in the ajax object to jQuery's $.ajax function, or the transport function you specify.

使用jQuery 2+,我是能够成功地设置的OAuth 2.0和Content-Type头。

Using JQuery 2+, I was able to successfully set OAuth 2.0 and Content-Type headers.

ajax: {
    headers: {
        "Authorization" : "Bearer "+Cookies.get('accessToken'),
        "Content-Type" : "application/json",
    },
}

这篇关于传递自定义页眉到Ajax请求上选择二的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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