简体   繁体   English

如何将标头添加到jqueryui自动完成调用?

[英]How do I add headers to a jqueryui autocomplete call?

I'm trying to add a http request-header to a jqueryui autocomplete request. 我正在尝试将http请求标头添加到jqueryui自动完成请求中。 I've looked at the documentation on the jquery site and the solution is presented there for ajax requests. 我查看了jquery站点上的文档,并在那里提供了针对ajax请求的解决方案。 I assumed the solution would be similar in my case but I can't get the darn thing to work. 我认为解决方案在我的情况下是类似的,但我不能让工作变得糟糕。

Here is my code. 这是我的代码。 It's wrapped in an angularjs diretive but the call inside the "link" method would be the same without it being inside the directive. 它包含在angularjs中,但是“link”方法中的调用将是相同的,而不是在指令内。

app.directive("buildingSearch", function () {
    // I bind the $scope to the DOM behaviors.
    function link(scope, element, attributes, controllers) {
        //Attach the autocomplete functionto the element
        element.autocomplete({
            source: 'api/building/unitOrgMdl',
            minLength: 2,

            //*********
            //This is the addition I made as per the jquery documentation that I figured would work but doesn't
            headers: {
                'Authorization': '122222222222'
            },
            //*********

            select: function (event, ui) {
                element.val(ui.item.label);
                element.blur();
                scope.getbuildings({ data: ui.item })
                return false;
            }
        });
    }

    // Return the directive confirugation.
    return ({
        link: link,
        restrict: "EA",
        replace: true,
        scope: {
            getbuildings: '&'
        }
    });
});

I figured it out... and it works great! 我想通了......它很棒! I cannot ensure it's the best way to do it but it seems pretty solid. 我不能确保它是最好的方法,但看起来非常稳固。 I added the following code right before the autocomplete() method. 我在autocomplete()方法之前添加了以下代码。

$.ajaxSetup({
    headers: { 'Authorization': '122222222222' }
});

So the new code in it's entirety looks like this. 所以新代码就像这样。

app.directive("tmBuildingSearch", function (authService) {
    // I bind the $scope to the DOM behaviors.
    function link(scope, element, attributes, controllers) {
        //Attach the autocomplete function to the element
        //NEW CODE. This attaches a custom header
        $.ajaxSetup({
            headers: { 'Authorization': '122222222222' }
        });

        element.autocomplete({
            source: 'api/building/unitOrgMdl',
            minLength: 2,
            select: function (event, ui) {
                element.val(ui.item.label);
                element.blur();
                scope.getbuildings({ data: ui.item })
                return false;
            }
        });
    }

    // Return the directive configuration.
    return ({
        link: link,
        restrict: "EA",
        replace: true,
        scope: {
            getbuildings: '&'
        }
    });
});

I got the idea from another post on Stake Overflow. 我从另一篇关于Stake Overflow的帖子中得到了这个想法。

How can I add a custom HTTP header to ajax request with js or jQuery? 如何使用js或jQuery向ajax请求添加自定义HTTP标头?

I hope this helps someone else! 我希望这有助于其他人!

ENHANCEMENT NOTE! 增强注意! I removed this added code from the directive and put it in the main app module so ANY ajax call will get the Authorization header added to it. 我从指令中删除了这个添加的代码并将其放在主应用程序模块中,因此任何ajax调用都会将Authorization标头添加到其中。 Works great! 效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM