我有一个简单的 Java 应用程序,它对 Web 服务请求使用react。
当用户按下按钮时,一条消息将发送到 Java 应用程序。
如果消息是“ping”,则应用程序将响应“pong”,否则 -“未知消息”。
Java 应用程序的代码:
@Path("/ping-pong")
public class PingPongService {
@POST
@Produces("text/plain")
public String test(@FormParam("message") final String aMessage) {
System.out.println("message from client: " + aMessage);
if ("ping".equalsIgnoreCase(aMessage)) {
System.out.println("ping.equalsIgnoreCase(aMessage)");
return "pong";
} else {
System.out.println("Unknown message");
return "unknown message " + aMessage;
}
}
}
在 WAR 文件中,我有以下 HTML 代码,该代码可以工作(当我按下其中一个按钮时,我会从 Java 应用程序获得响应,并将其显示在消息框中):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JavaScript Ping-Pong Client</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
//This function send post request to the server using jQuery
function testWithJQuery(message){
$.post('http://localhost:8345/rest/ping-pong', { message: message }, function(data) {
alert(data);
});
}
//This function send post request to the server using a low-level XmlHttpRequest instead of the jQuery
//(no dependencies on external libraries)
function test(message){
var http = new XMLHttpRequest();
var url = "http://localhost:8345/rest/ping-pong";
var params = "message=" + message;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
};
http.send(params);
}
</script>
</head>
<body>
<button onclick="test('ping')">Test</button>
<button onclick="testWithJQuery('ping')">Test using jQuery</button>
</body>
</html>
我想在 Django 应用程序中执行完全相同的操作(用户按下按钮,消息发送到 Java 应用程序,其响应显示在警报消息窗口中)。
Django 生成以下 HTML 代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
//This function send post request to the server using jQuery
function testWithJQuery(message){
$.post('http://localhost:8345/rest/ping-pong', { message: message }, function(data) {
alert(data);
});
}
//This function send post request to the server using a low-level XmlHttpRequest instead of the jQuery
//(no dependencies on external libraries)
function test(message){
var http = new XMLHttpRequest();
var url = "http://localhost:8345/rest/ping-pong";
var params = "message=" + message;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
};
http.send(params);
}
</script>
</head>
<body>
<button onclick="test('ping')">Test</button>
</body>
</html>
当我在 Django 生成的代码中按下“测试”按钮时,Java 应用程序会收到消息(我在控制台输出中看到这一点),但 Java 应用程序的响应不会显示在 Django 网站中,即使 HTML 代码与 WAR 文件中的代码相同。
我该如何解决这个问题,i。 e.确保 Java 应用程序的响应显示在 Django 中?
UPD:以下是 Chrome 的“网络” Pane 中的详细错误说明:
最佳答案
这不是一个直接的答案,而是一些帮助您诊断问题的提示;我在这里发帖是因为它太大而无法发表评论。
- 打开 Chrome 调试器 (shif+ctrl+i) 或 Firefox Firebug 并激活“网络”选项卡。
- 检查两个版本发布的帖子并比较标题和正文
如果存在明显的行为,则两个帖子之间应该存在一些差异。
[更新]
I found the error - see the screenshot in the question. The error is "XMLHttpRequest cannot load localhost:8345/rest/ping-pong. Origin localhost:8000 is not allowed by Access-Control-Allow-Origin.".
该错误是由于浏览器中的安全限制 ( same origin policy ) 造成的。有一些解决方法(来源:维基百科):
To enable developers to, in a controlled manner, circumvent the same origin policy, a number of "hacks" such as using the fragment identifier or the window.name property have been used to pass data between documents residing in different domains. With the HTML5 standard, a method was formalized for this: the postMessage interface, which is only available on recent browsers. JSONP and cross-origin resource sharing can also be used to enable ajax-like calls to other domains.2 easyXDM can also be used to easily work around the limitation set in place by the same origin policy. It is a lightweight, easy-to-use and self-contained Javascript library that makes it easy for developers to communicate and expose javascript APIs across domain boundaries.
我用过 subdomain hack成功后,请参阅以下答案:
关于java - Django 中的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11660013/