Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova Angular Get to Web Api Connection Refused

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>
like image 631
Sealer_05 Avatar asked Nov 25 '14 05:11

Sealer_05


2 Answers

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.

like image 113
Sealer_05 Avatar answered Oct 11 '22 16:10

Sealer_05


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.com/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

like image 6
Erin Geyer Avatar answered Oct 11 '22 17:10

Erin Geyer