gpt4 book ai didi

javascript - 从代理服务器后面连接到其余 Web 服务时出错

转载 作者:行者123 更新时间:2023-12-02 13:36:05 25 4
gpt4 key购买 nike

我在从代理服务器后面连接到我的其余 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)

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

干杯,

解决方案

经过 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
};

最重要的问题是

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

希望这对其他人有帮助,

干杯,

最佳答案

“Localhost”是一个特殊定义的名称,它解析为 127.0.0.1,这是环回地址,并且只能在同一台计算机上使用,不能通过任何其他网络访问。

如果您的 URL 以“localhost”作为权限,则它们只能在与服务器相同的计算机上运行。

关于javascript - 从代理服务器后面连接到其余 Web 服务时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42995330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com