最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cordova Angular Get to Web Api Connection Refused - Stack Overflow

programmeradmin8浏览0评论

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%2522method%2522%3A%2522GET%2522%2C%2522array%2522%3Atrue%257D net::ERR_CONNECTION_REFUSED

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!

Here is what the angular get looks like

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

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

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

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%2522method%2522%3A%2522GET%2522%2C%2522array%2522%3Atrue%257D net::ERR_CONNECTION_REFUSED

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!

Here is what the angular get looks like

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

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

 <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
</httpProtocol>
Share Improve this question edited Aug 24, 2016 at 14:18 Devid Farinelli 7,5549 gold badges44 silver badges74 bronze badges asked Nov 25, 2014 at 5:10 Sealer_05Sealer_05 5,5668 gold badges37 silver badges56 bronze badges 2
  • have you whitelisted the api domain in your cordova config.xml. see cordova.apache/docs/en/edge/… – Amitesh Commented Nov 25, 2014 at 5:28
  • There was an asterisk there so I assume all were white listed. I also just explicitly added the web api localhost but no luck. Any other suggestions? – Sealer_05 Commented Nov 25, 2014 at 5:36
Add a ment  | 

2 Answers 2

Reset to default 12

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.

The answer as it pertains to issues with accessing http://localhost (which is the same thing as 127.0.0.1) can be found here: http://developer.android./tools/devices/emulator.html - which says:

"Also note that the address 127.0.0.1 on your development machine corresponds to the emulator's own loopback interface. If you want to access services running on your development machine's loopback interface (a.k.a. 127.0.0.1 on your machine), you should use the special address 10.0.2.2 instead."

So, instead of using http://localhost use http://10.0.2.2 and then append whatever port you're using. Example: http://localhost:8001 can be access at http://10.0.2.2:8001

发布评论

评论列表(0)

  1. 暂无评论