Java Jquery脚本调用Servlet-can';无法获取Servlet的正确URL
下面是jquery脚本,它通过URL调用servlet 我有两个独立的项目 首先是包含servlet等的动态Web项目 第二个是简单的Ratchet HTML-CSS-JS项目,其中当然包含一些页面、脚本和CSSJava Jquery脚本调用Servlet-can';无法获取Servlet的正确URL,java,javascript,jquery,css,servlets,Java,Javascript,Jquery,Css,Servlets,下面是jquery脚本,它通过URL调用servlet 我有两个独立的项目 首先是包含servlet等的动态Web项目 第二个是简单的Ratchet HTML-CSS-JS项目,其中当然包含一些页面、脚本和CSS <script> $(document).ready(function() { // When the HTML DOM is ready loading, then execute th
<script>
$(document).ready(function() { // When the HTML DOM is ready loading, then execute the following function...
$('#button').click(function() { // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event...
$.get('http://localhost:8080/testuje/text', function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
$('#div').text(responseText); // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
});
});
});
</script>
问题是,我应该在
$中输入什么http://localhost:8080/testuje/text,函数(responseText)
单击按钮后获取servlet内容。粘贴http://localhost:8080/testuje/text
直接进入浏览器的地址栏?如果是这样,请告知包含试图访问该URL的jQuery Ajax代码的页面的完整URL。很可能这只是CORS问题。您可以发布您的web控制台吗输出?看起来像是跨域/跨源问题。
package pl.javastart.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/text")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "some text";
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
response.getWriter().write(text);
// Write response body.
}
}