Search code examples
javascriptjavaajaxresthttp-proxy

Error connecting to a rest webservice from behond a proxy server


I am having a problem connecting to my rest web service from behind a proxy server. I have the following ajax call

 $.ajax({       
        async: false,
        url: "http://localhost:8080/Card/rest/UploadBackground"       
    }).done(function ( data ) {
        arr = JSON.parse(data);  
        updateSlider(arr);
    }).fail(function ( data ) {
      alert('falied');
   });        

which calls the following service

@GET   
@Produces(MediaType.TEXT_PLAIN)
public Response getBackgroundImages(@QueryParam("missions") String missions, 
                                  @QueryParam("objects") String objects, 
                                  @QueryParam("dates") String date) {

   String result= ""; 
   ......
   try {
         .....   
       result = dbman.extractQueriedData(queryMissions, queryObjects, date);
        } else {
            result = dbman.extractPopular();           
        }

   } catch(SQLException ex){
      LoggingServices.createExceptionLogMessage(ex, LOGGER);     
   }
  return Response.ok() 
        .entity(result)
        .header("Access-Control-Allow-Origin", "*")
        .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
        .allow("OPTIONS").build();  

}

The code works when I am running everything on a local server. But when I upload it to the server that is behind a proxy server I get an error, which according to chrome net-internals page is as so

245: URL_REQUEST
http://localhost:8080/Card/rest/UploadBackground
Start Time: 2017-03-23 16:23:19.477

t=12380 [st=   0] +REQUEST_ALIVE  [dt=1305]
                   --> priority = "HIGHEST"
                   --> url = "http://localhost:8080/Card/rest/UploadBackground"
t=12380 [st=   0]    URL_REQUEST_DELEGATE  [dt=0]
t=12380 [st=   0]   +URL_REQUEST_START_JOB  [dt=1305]
                     --> load_flags = 51008 (DO_NOT_SAVE_COOKIES | DO_NOT_SEND_AUTH_DATA | DO_NOT_SEND_COOKIES | IGNORE_LIMITS | MAYBE_USER_GESTURE | VERIFY_EV_CERT)
                     --> method = "GET"
                     --> url = "http://localhost:8080/Card/rest/UploadBackground"
t=12380 [st=   0]      URL_REQUEST_DELEGATE  [dt=0]
t=12380 [st=   0]      HTTP_CACHE_GET_BACKEND  [dt=0]
t=12380 [st=   0]      HTTP_CACHE_OPEN_ENTRY  [dt=0]
t=12380 [st=   0]      HTTP_CACHE_ADD_TO_ENTRY  [dt=0]
t=12380 [st=   0]      HTTP_CACHE_READ_INFO  [dt=0]
t=12380 [st=   0]     +HTTP_STREAM_REQUEST  [dt=303]
t=12380 [st=   0]        HTTP_STREAM_REQUEST_STARTED_JOB
                         --> source_dependency = 248 (HTTP_STREAM_JOB)
t=12683 [st= 303]        HTTP_STREAM_REQUEST_BOUND_TO_JOB
                         --> source_dependency = 248 (HTTP_STREAM_JOB)
t=12683 [st= 303]     -HTTP_STREAM_REQUEST
t=12683 [st= 303]     +HTTP_TRANSACTION_SEND_REQUEST  [dt=0]
t=12683 [st= 303]        HTTP_TRANSACTION_SEND_REQUEST_HEADERS
                         --> GET /Card/rest/UploadBackground HTTP/1.1
                             Host: localhost:8080
                             Connection: keep-alive
                             Accept: */*
                             Origin: http://mycard.xxx.yyy
                             User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36
                             Referer: http://mycard.xxx.yyy/mainPage.html
                             Accept-Encoding: gzip, deflate, sdch, br
                             Accept-Language: en-US,en;q=0.8
t=12683 [st= 303]     -HTTP_TRANSACTION_SEND_REQUEST
t=12683 [st= 303]     +HTTP_TRANSACTION_READ_HEADERS  [dt=1002]
t=12683 [st= 303]        HTTP_STREAM_PARSER_READ_HEADERS  [dt=1002]
                         --> net_error = -324 (ERR_EMPTY_RESPONSE)
t=13685 [st=1305]     -HTTP_TRANSACTION_READ_HEADERS
                       --> net_error = -324 (ERR_EMPTY_RESPONSE)
t=13685 [st=1305]   -URL_REQUEST_START_JOB
                     --> net_error = -324 (ERR_EMPTY_RESPONSE)
t=13685 [st=1305]    URL_REQUEST_DELEGATE  [dt=0]
t=13685 [st=1305] -REQUEST_ALIVE
                   --> net_error = -324 (ERR_EMPTY_RESPONSE)

can someone explain what the error is? has this something to do with the proxy server?

cheers,

es

SOLUTION

After more than 4 days I finally got it working. The problem was simply cross domain issue and the way I solved it was to use jsonp and the with correct url mapping. On the server side I changed the MediaType to MediaType.APPLICATION_JSON and the return value was wrapped around the callback function name, see below

@GET   
@Produces( MediaType.APPLICATION_JSON)  
public String getBackgroundImages(@QueryParam("missions") Stringmissions, 
                                  @QueryParam("objects") String objects, 
                                 @QueryParam("dates") String date) {

 String result= ""; 
 ......
 try {
       .....   
       result = dbman.extractQueriedData(queryMissions, queryObjects,   date);
    } else {
        result = dbman.extractPopular();           
    }

    } catch(SQLException ex){
       LoggingServices.createExceptionLogMessage(ex, LOGGER);     
    }
      return String res = "jsonCallback("+ result +");"; 

    }

and on the javascript side

$.ajax({       
        async: false,
        url: "/rest/UploadBackground/",       
		 jsonp: "callback",
           type:"GET",
           dataType: "jsonp",
           jsonpCallback: "jsonCallback",
           success: function( response ) {                 
           }              
    
 });    
 
 window.jsonCallback = function(data) {  
     //do something
};

The most importing issues are

  • The server return value must be wrapped by the callback function name.
  • The callback function must be defined globally with the given signature, as far as I know.

Hope this will help other,

cheers,

es


Solution

  • "Localhost" is a specially defined name, which resolves to 127.0.0.1 which is the loopback address, and is only available from the same machine, not accessible across any other network.

    If your URLs have "localhost" as the authority, they will only work from the same computer as the server.