【问题标题】:how can i convert my curl command to angularjs http request如何将我的 curl 命令转换为 angularjs http 请求
【发布时间】:2017-02-09 19:01:29
【问题描述】:

需要将以下 curl 命令转换为 angular http 请求。 命令看起来类似于下面的命令。

" curl -X POST --data 'username=myusername&password=mypassword' -i https://myurl.com/j_spring_security_check "

我想在 https 中使用 GET 或 POST。任何人都可以在这方面提供帮助。

【问题讨论】:

    标签: angularjs curl-commandline


    【解决方案1】:

    它可能是一个具有postMethod 方法的服务,该方法接受两个参数用户名和密码,就像这样:

    angular.factory('myService', ['$http', function($http){
        function postMethod(username, password){
            var request = {
                method:'POST',
                url: 'https://myurl.com/j_spring_security_check ',
                data: {
                    username:username,
                    password:password
                }
            }
            return $http(request);
        }
    
        return {
            postMethod:postMethod
        }
    ]);
    

    然后您可以使用以下服务:

    angular.module('myapp').controller('MainCtrl', ['myService', function(myService){
        myService.postMethod('foo', 'bar').then(function(response){
            // Do a UI change ?
        });
    }]);
    

    【讨论】:

    • 感谢老兄的回复。以类似的方式尝试过。我收到以下错误...“对预检请求的响应未通过访问控制检查:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。因此不允许访问来源'localhost:9292'” .我该如何解决这个错误?
    • CORS 问题...如果您使用的是 chrome,最简单的方法是安装以下扩展程序以允许多源请求:chrome.google.com/webstore/detail/allow-control-allow-origi/…
    • 问题已在我的机器中修复。非常感谢扎卡里亚。但我在这里有一点疑问。我无法在用户机器上安装这个 chrome 扩展。那么如何解决这个问题。
    • 最常见的方法是在服务器端限制来源。欲了解更多信息:enable-cors.org/server.html
    猜你喜欢
    • 2016-11-25
    • 2016-08-04
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多