AngularJs与基本身份验证

时间:2015-05-14 00:19:52

标签: angularjs api rest http curl

我想使用montastic的Restful API在我的AngularJS网站上实现一些功能。但不幸的是,我无法连接到montastic。 montastic API在这里https://github.com/montastic/montastic-api-doc

我必须在angularjs中使用这个卷曲 卷曲-H'接受:application / json' -H'内容类型:application / json' -u daniel@metadot.com:123456 https:// montastic / checkpoints / index 使用我在base64中编码的用户密码 我测试了卷曲,它适用于hurl.it

要将我连接到angularJS中的montastic,我使用此脚本:

var app = angular.module('myApp', []);
enter code here
app.controller('MainCtrl', function($scope, $http) {
  $http.defaults.headers.common['Authorization']= 'Basic ';

  $http.defaults.headers.common['Authorization']= 'Basic c3RlcGhhbmUuY2F5c3NpYWxzQGdtYWlsLmNvbTptZXRhZG90Njk=';
  $http.defaults.headers.common['Accept']= "application/json";
  $http.defaults.headers.common['Content-Type']= "application/json";
  $http({
    url : "https://www.montastic.com/checkpoints/index",
    method : 'GET',
    })
  .success(function(status, data) {
    $scope.status = status;
    $scope.data = data;
      $scope.result = "ok";
  }).error(function(data) {
      $scope.result = "ko";
  });
});

所以我收到了这条消息:

  

XMLHttpRequest无法加载https:// montastic / checkpoints / index。该请求被重定向到' https://***montastic.com/login' ;,这对于需要预检的跨源请求是不允许的。

Response Headers
view source
Cache-Control:no-store
Connection:close
Content-Length:97
Content-Type:text/html; charset=utf-8
Date:Thu, 14 May 2015 00:00:08 GMT
Location:https://***montastic***/login
Server:Apache/2.2.3 (Red Hat)
Set-Cookie:_montastic_session=6d2c33c9c403301594d943488910a33c; path=/; HttpOnly
Status:302 Found
Vary:*
X-Powered-By:Phusion Passenger 4.0.53
X-Runtime:2
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:www.montastic.com

你可以在这里看到plunkr上的代码 http://plnkr.co/edit/real1uVBJH7MzvgSb1Mo?p=preview

编辑我:

感谢Phil,我意识到我必须使用自己的服务器端代理cors angularjs。但我是AngularJS的初学者,我不知道如何构建它。我在互联网上搜索,但所有解决方案似乎都难以开发。

你知道吗? 请帮帮我:)。

0 个答案:

没有答案
相关问题