Connection refused when running on device


#1

Hi all

Am calling a json service on a local weblogic server. When runned on a local browser i am able to retrieve my json response. But when runned on my device with the command: “ionic run android --device” am getting the following error:

Failed to load resource: net::ERR_CONNECTION_REFUSED

My code:

$scope.url = "http://localhost:7101/appdk03_portal_
$http.get($scope.url).then(function (response) {…

I also tried changing the url like this:

$scope.url = "http://10.0.2.2:7101/appdk03_portal_
$http.get($scope.url).then(function (response) {…

I guess it only works running on a emulator. I want to run my app in my device.

Any help with be appreciated


#2

I think its CORS issue , You need to enable CORS in your API.


#3

Can u describe in more details how i am suppose to setup CORS in my API. Maybe links?


#4

Which platform you are using for design API ?


#5

Read these articles for enable CORS

For asp.net - http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

For php - http://stackoverflow.com/questions/8719276/cors-with-php-headers


#6

Thx it worked by setting http default post content type:

$http.defaults.headers.post[“Content-Type”] = “application/x-www-form-urlencoded”;

Then on the weblogic server setting up a filter:

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class SimpleCORSFilter implements Filter {

@Override
public void init(FilterConfig fc) throws ServletException {
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse)res;
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.addHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "X-ACCESS_TOKEN, Access-Control-Allow-Origin, Authorization, Origin, x-requested-with, Content-Type, Content-Range, Content-Disposition, Content-Description");
    chain.doFilter(req, res);
}

@Override
public void destroy() {
}

}

changing web.xml:

filter>
filter-name>corsFilter
filter-class>dk.xxxxxxx.portal.filters.SimpleCORSFilter
/filter>

filter-mapping>
filter-name>corsFilter
servlet-name>MyService
/filter-mapping>