Issue
I am learning working with Servlets and JavaScript, by trying to build an application where a Java servlet and a JavaScript code can exchange data. I have built a simple WebPage with an Header and a button. Upon Clicking the button, the client should send an AJAX request to the server, and the Server should send some data as a response, that should alter the text on the Header accordingly. I am using jQuery to send and receive the data, and alter the text on the Header. Here is the HTML code :
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>$(function() { alert('hello') });</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="Try.js"></script>
</head>
<body>
<h1 align = "center" id="header">Using Visual Studio</h1>
<button id="button">Click</button>
</body>
</html>
This is the Try.js JavaScript code with jQuery :
$(function(){
$("button").click(function(){
$.ajax({url: "localhost:8080/Ajax/Serv", success: function(result){
$("#header").html(result);
}});
});
});
And following is the servlet code:
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;
import jdk.nashorn.internal.parser.JSONParser;
@WebServlet("/Serv")
public class Serv extends HttpServlet {
private static final long serialVersionUID = 1L;
public Serv() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
response.getWriter().write("Success Data!!!");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
The webpage Header should display "Success Data!!!" on Clicking the button "Click". However that is not happening. I am using TomCat 7.0.39 on port 8080 for the server.
Solution
It appears you are loading the html page from your file system (e.g. file:///C:/Ajax/serv.html) and not the localhost (e.g. http://localhost:8080/Ajax/serv.html). That's why you're getting the Cross Origin Request Blocked error when attempting the Ajax call, this is a Cross-site Scripting (XSS) security vulnerability that your browser is preventing.
You should move your html (js, etc) file under a new Tomcat webapps folder, such as webapps/Ajax) and load them using http://localhost:8080/Ajax/serv.html. This will prevent the XSS problem.
Also adding an error handler to the Ajax call will help determine the problem.
$("button").click(function(){
$.ajax({url: "localhost:8080/Ajax/Serv",
success: function(result){
$("#header").html(result);
},
error: function(xhr, status, ex) {
alert("state=" + xhr.state() + ", status=" + status);
}
});
Answered By - Steve M
Answer Checked By - Terry (JavaFixing Volunteer)