从behond代理服务器

时间:2017-03-24 09:06:46

标签: javascript java ajax rest http-proxy

我在从代理服务器后面连接到我的其他Web服务时遇到问题。我有以下ajax调用

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

调用以下服务

@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();  

}

当我在本地服务器上运行所有内容时,代码可以正常工作。但是当我将它上传到代理服务器后面的服务器时,我得到一个错误,根据chrome net-internals页面是这样的

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)

有人可以解释错误是什么吗?这与代理服务器有关吗?

欢呼声,

ES

超过4天后,我终于开始工作了。问题只是跨域问题,我解决它的方法是使用jsonp和正确的url映射。在服务器端,我将MediaType更改为MediaType.APPLICATION_JSON,返回值包含在回调函数名称中,见下文

@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 +");"; 

    }

并在javascript端

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

导入最多的问题是

  • 服务器返回值必须由回调函数名称包装。
  • 据我所知,必须使用给定的签名全局定义回调函数。

希望这会有助于其他人,

欢呼声,

es

1 个答案:

答案 0 :(得分:1)

"本地主机"是一个特殊定义的名称,它解析为127.0.0.1这是环回地址,并且只能从同一台机器上获得,不能通过任何其他网络访问。

如果您的网址有" localhost"作为权限,它们只能在与服务器相同的计算机上工作。