2014-06-14 45 views
2

我正尝试连接到家中的家庭自动化服务器上​​的RESTFul API(因此不需要大规模安全性)。当我尝试使用没有凭据连接,我得到:Angular Digest Authentication

[Error] XMLHttpRequest cannot load http://localhost:8176/devices/. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 

文档说:

靛蓝使用 “消化authentificaction”(没有基本也不令牌)

卷曲-X PUT - -digest -u {username}:{password} -d value = {value} http:// {IPAddress}:8176/variables/{VariableToChange}

curl -X PUT --digest -u admin:p @ ssw0rd -d value = true http:// {IPAddress}:8176/variables/Var1

要获得设备的列表,我可以用这3个之一:

http://127.0.0.1:8176/devices/ 
http://127.0.0.1:8176/devices.xml/ 
http://127.0.0.1:8176/devices.txt/ 

我就如何通过信条全盘损失。这里是我的代码到目前为止:

function DeviceController($scope, $http) { 
    $http.get("http://localhost:8176/devices/") 
     .then(function (results) { 
      //Success; 
      console.log("Succss: " + results.status); 
      $scope.devices = results.data; 
     }, function (results) { 
      //error 
      console.log("Error: " + results.data + "; " 
            + results.status); 
      $scope.error = results.data; 
     }) 
}; 

任何人都可以指出我在正确的方向吗?

感谢

马克

+0

我觉得家庭自动化服务器需要你能想到的尽可能多的安全性。 – icebreaker

+0

@icebreaker - 不是在安全的本地网络上时 – mark1234

回答

2

您的服务器具有配置为接受CORS (Cross-origin resource sharing)

编辑: 它看起来像你的靛蓝(不知道它是什么)服务器是不是你的角度应用服务。请参阅您的服务器文档,了解如何启用CORS。

至于你的请求期间传递您的凭据,做到:

$http({ 
    method: "GET", 
    url: "http://localhost:8176/devices/", 
    headers: {"Authorization": "Basic " + btoa("admin:[email protected]")}, 
})... 
+0

@gframontina感谢您的回复。服务器将最终成为OS X上的apache。现在我正在Visual Studio中开发,因此它是asp网络服务器。我发现我必须使用摘要授权,我可以只交换摘要的“基本”? (我猜不是:-(。问候马克 – mark1234

+0

快速搜索角+摘要给了我几个模块,帮助处理整个过程:[https://github.com/PatrickHeneise/angular-digest-interceptor ](https://github.com/PatrickHeneise/angular-digest-interceptor)和[https://github.com/tafax/angular-digest-auth](https://github.com/tafax/angular-digest -auth)我没有使用任何这些,但他们似乎使任务更容易。就配置服务器来启用CORS,我没有太多的.NET世界的经验,但我向你保证是可能的。:-) – gtramontina

+0

感谢您的回复。我已经关闭auth现在,但CORS是杀了我。我用VS开发,所以我的“网络服务器”是asp.net web服务器VS开始用于调试。我把这个添加到web.comfig,但仍然没有运气。 mark1234