使用用户名密码的Angularjs Rest端点

时间:2014-11-29 19:58:48

标签: javascript angularjs rest curl ngresource

我正在尝试编写基于AngularJS的单页应用程序(SPA)。应用程序应连接到提供RestFul服务的Web服务器,但每个端点都需要用户名和密码以及其他参数。由于我是这个领域的初学者,在开始实际开发之前,我尝试使用PostMan / Advanced Rest Client chrome扩展来验证基本连接。示例请求预览:

POST /servicesNS/admin/search/search/jobs/export HTTP/1.1
Host: localhost:8089
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

search=search+error+|+table+host&output_data=xml&username=admin&password=unity3d

这实际上等同于cURL命令:

curl  -k -u admin:unity3d --data-urlencode search="search error | table host" -d "output_mode=xml" https://localhost:8089/servicesNS/admin/search/search/jobs/result

在以上述方式获得成功后,我现在正在寻找在AngularJS中执行此操作的等效方法。

var app = angular.module('TabsApp', []);
app.controller('TabsCtrl', function ($scope, $http)
{
    login = function () {

        $scope.userName ="admin";
        $scope.password ="unity3d"

        $http({
            method :"POST",
            url:"https://localhost:8089/servicesNS/admin/search/search/jobs/export",
            data: { "username" :  "admin" , "password": "unity3d", "search" : "search error"},
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}

        }).success(function (data, status, headers, config) {
            console.log('status',status);
            console.log('data',status);
            console.log('headers',status);
        });
    }
});

这给了我错误401 Unauthorized,响应的标题:

> Remote Address:127.0.0.1:8089 Request
> URL:https://localhost:8089/servicesNS/admin/search/search/jobs/export
> Request Method:POST Status Code:401 Unauthorized Request Headersview
> source Accept:application/json, text/plain, */* Accept-Encoding:gzip,
> deflate Accept-Language:en-US,en;q=0.8 Connection:keep-alive
> Content-Length:65 Content-Type:application/x-www-form-urlencoded
> Host:localhost:8089 Origin:http://localhost:63342
> Referer:http://localhost:63342/UI/UI1.html User-Agent:Mozilla/5.0
> (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
> Chrome/39.0.2171.71 Safari/537.36 Form Dataview sourceview URL encoded
> {"username":"admin","password":"unity3d","search":"search error"}:
> Response Headersview source Access-Control-Allow-Credentials:true
> Access-Control-Allow-Headers:Authorization
> Access-Control-Allow-Methods:GET,POST,PUT,DELETE,HEAD,OPTIONS
> Access-Control-Allow-Origin:* Cache-Control:private
> Connection:Keep-Alive Content-Length:130 Content-Type:text/xml;
> charset=UTF-8 Date:Sat, 29 Nov 2014 19:53:59 GMT Server:Splunkd
> Vary:Cookie, Authorization WWW-Authenticate:Basic realm="/splunk"
> X-Content-Type-Options:nosniff X-Frame-Options:SAMEORIGIN

输出是:

<?xml version="1.0" encoding="UTF-8"?> <response>   <messages>
    <msg type="ERROR">Unauthorized</msg>   </messages> </response>

知道出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

如果您以'application/x-www-form-urlencoded'的格式发送回复,则实际数据格式应相同。

您当前发送的内容是JSON个对象。您需要使用$http tranformer来转换请求:符合

的内容
            transformRequest: function (data) {
                var postData = [];
                for (var prop in data)
                postData.push(encodeURIComponent(prop) + "=" + encodeURIComponent(data[prop]));
                return postData.join("&");
            },

在这里查看工作小提琴http://jsfiddle.net/cmyworld/doLhmgL6/