简体   繁体   English

如何在Angular中执行带有请求标头的Ajax发布

[英]How do I execute an ajax post with request header in Angular

I am having a working code in jquery, where I post data from form with a token using request.setRequestHeader("X-CSRF-Token", $.cookie('token')); 我在jquery中有一个工作代码,在这里我使用request.setRequestHeader("X-CSRF-Token", $.cookie('token'));从带有令牌的表单中发布数据request.setRequestHeader("X-CSRF-Token", $.cookie('token')); . I know how to get the form data into a $scope using ng-model , but how do I convert the submit_this() function below to an angular compatible function? 我知道如何使用ng-model将表单数据获取到$scope ,但是如何将下面的submit_this()函数转换为角度兼容函数?

The Jquery Code: jQuery代码:

$("body").on("click", "#login", function() {

        var url = "http://localhost/lab/theapp/api/user/login.json";    
        var name = $('#user').val();

        var pass = $('#pass').val();

        var data = 'username=' + encodeURIComponent(name) + '&password=' + encodeURIComponent(pass);
        var type = "post";

        submit_this(type,data,url);

    }); 


    //the function that need to be re-written for AngularJs   
    function submit_this(type,data,url) {
            try {   

                $.ajax({
                        url: url,
                        type: type,
                        data: data,
                        dataType: 'json',
                        beforeSend: function (request) {
                        request.setRequestHeader("X-CSRF-Token", $.cookie('token'));
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {

                        },
                        success: function (data) {
                            if (data.sessid){   
                                var sessid = data.sessid;
                                var session_name = data.session_name;
                                var token = data.token;
                                jQuery.cookie("token", token);
                            }
                            console.log(data);
                        }
                });
            }   
        catch (error) { console.log(error) }
        return false;   
}

You can use $http service to do that. 您可以使用$ http服务执行此操作。 You'd better create you own service, Demo code: 最好创建自己的服务,演示代码:

var demoApp = angular.module("DemoApp");
demoApp.controller("yourcontroller", function($scope, yourService){
    yourService.postData("http://your/url", {"X-CSRF-Token": $scope.token})
        .success(function(data){
            //handle success response here
        }).error(function(error){
            //handle error response here 
        });                                  
});
demoApp.service("yourService", function($http){
    this.postData = function(url, _headers){
        return $http({
                  method: "POST", 
                  url: url,
                  headers: _headers
               });
     };
});

Here is angular $http document. 这是有角度的$ http文件。

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

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