本文介绍了Cordova Angular拒绝网络Api连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2013 Update 4,我有一个web api项目设置来接收返回数组的get请求。对于客户端应用程序我已设置cordova并使用ng-resource模拟一个Android角度应用程序来调用web api get。每次我调用GET时都会收到ripple.js错误,表示连接被拒绝。即使我尝试使用真正的Android设备,我也会拒绝连接。这是使用纹波仿真器时的错误

I am using Visual Studio 2013 Update 4 and I have a web api project setup to receive get requests that returns an array. For the client app I have setup cordova and am emulating an android angular app using ng-resource to call the web api get. Everytime I call the GET I get a ripple.js error that says connection refused. I get the connection refused even if I try with a real android device as well. Here is the error when using the ripple emulator

OPTIONS http:// ****:4400 / ripple / xhr_proxy?tinyhippos_apikey = ABC& tinyhippos_rur ... Fget%3D%257B%2522方法%2522%3A%2522GET%2522%2C%2522array%2522%3Atrue%257D net :: ERR_CONNECTION_REFUSED

我确信在web api 2服务器上启用了cors,因为cordova和web api项目是同一本地主机上的不同端口号。我通过角网页创建了一个完全复制的cordova角应用程序,不仅证明了cors功能,还证明了代码。我也尝试过postman并且都正确地得到了json的反应。只有cordova Android应用程序给我连接被拒绝。任何帮助将不胜感激!

I was sure to enable cors on the web api 2 server since the cordova and web api projects are different port numbers on the same local host. I proved not only the cors functionality but also the code by creating an exact replica of the cordova angular app with just angular web page. I also tried with postman and both get json responses correctly. It is only the cordova android app that is giving me the connection refused. Any help would be greatly appreciated!

这是角度获得的样子

app.factory('mrMaintService', function ($resource) {
    return $resource('http://localhost:15528/api/requests', {
        get: { method: 'GET', array: true }
    });
});

这表明我允许web api项目中的所有域名:

This shows I am allowing all domains on the web api project:

 <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
</httpProtocol>


推荐答案

解决方法是从drop中禁用跨域代理在模拟器中向右下方。如果您不熟悉纹波仿真器,则很容易犯错误。

The solution was to disable cross domain proxies from the drop down right in the emulator. Easy mistake to make if you aren't familiar with the ripple emulator.

这篇关于Cordova Angular拒绝网络Api连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 02:29
查看更多