2

I have this Ajax query to a html page but it always fails. The html page is something like this only has a <h1> tab with some text.

Can anyone tell me why this is happening

$.ajax({
    url: "http://localhost:8080/FindMySize/src/main/webapp/WEB-INF/jsp/result.html",
    success: function(data) {
        $('#stage').html(data);
    },
    error: function(data) {
        $('#stage').html("AS");
    }

});
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Aman Kumar Sinha
  • 407
  • 6
  • 17

4 Answers4

0

add button click event before ajax call

$("button").click(function(){
  $.ajax({
   url: "http://localhost:8080/FindMySize/src/main/webapp/WEB-INF/jsp/result.html",
   success: function(data) {
     $('#stage').html(data);
   },
   error: function(data) {
     $('#stage').html("AS");
   }
 });
});
Dhaval Patel
  • 1,076
  • 6
  • 19
0

Add this line to your code

processData: false,
contentType: false,
type: "POST",

Also decide what you use GET or POST and write or type: "POST" or type: "GET"

http://www.w3schools.com/jquery/jquery_ajax_get_post.asp

Also put your code in a function and call it when click button for example

<button onclick="f();return false;">

function f() { /*your js code here*/ }
Michael
  • 1,089
  • 1
  • 11
  • 28
0
Try this one,     
   $.ajax
        ({
            url: "../FindMySize/src/main/webapp/WEB-INF/jsp/result.html",
            type: "GET",
            cache: false,
            async: false,    
            data: {},
            success: function(data) {
                alert("success");//to check whether it was working or not
                $('#stage').html(data);
            },
            error: function(data) {
    alert("error");
                $('#stage').html("AS");
            }

    });
0

Your javascript is OK.

The problem is your Java structure, the file result.html here has not relationship with jsp. You shouldn't put it inside WEB-INF. See this SO answer for more clear understanding.

The WEB-INF node is not part of the public document tree of the application. No file contained in the WEB-INF directory may be served directly to a client by the container

Solution: Move your html to public folder (where you store your javascript, css, img,..)

Community
  • 1
  • 1
haotang
  • 5,520
  • 35
  • 46