Hi I have been trying to make a ajax call to a JSP page. Here's the piece of JS function.
<script>
$(function(){
function myAjaxCall() {
$.ajax({
type: "post",
url: "jsp/common/myJavascriptPage.jsp",
dataType: "text",
success:
function (result) {
alert("Got the result: " + result);
},
error: function (xhr,status,error) {
alert("Status: " + status);
alert("Error: " + error);
alert("xhr: " + xhr.readyState);
},
statusCode: {
404: function() {
alert("page not found");
}
}
});
}
});
</script>
I am constantly getting file not found, even though JSP exists in the URL mentioned. Please note that I am calculating the JSP file location relative to that of webapp directory.
I tried using the normal AJAX calls (without jQuery), but ended up with same error.
Could you please help me understand why is it not able to locate the jsp?
The jQuery ajax fail is an ajax event which is only called if the request fails. The AJAX fail is a global event that triggered on the document to call handler function, which may be listening. The ajax fail can be performed with the help of the ajaxError () function. The jQuery ajaxError () function is a built-in function in jQuery.
If you run the code above, you will see that this is 404. If an Internal Server Error occurs, then the status property will be 500. statusText: If the Ajax request fails, then this property will contain a textual representation of the error that just occurred.
Attach the event handler to the document: $ ( ".log" ).text ( "Triggered ajaxError handler." ); Now, make an Ajax request using any jQuery method: When the user clicks the button and the Ajax request fails, because the requested file is missing, the log message is displayed.
Now, make an Ajax request using any jQuery method: When the user clicks the button and the Ajax request fails, because the requested file is missing, the log message is displayed. All ajaxError handlers are invoked, regardless of what Ajax request was completed. To differentiate between the requests, use the parameters passed to the handler.
Please note that I am calculating the JSP file location relative to that of webapp directory.
This is where you're going wrong.
AJAX is being executed from the client's web browser, meaning that it's not relative to anything on the server.
If you were to hit that page in a web browser, where you would go?
That's where you want your request URL to be.
Edit: I clearly didn't explain this well enough, so that's have another go!
Let's say I have a file on my server that's stored like so:
my_website/src/webapp/jsp/common/myFile.jsp
If I wanted to access this resource through a publicly accessible URL, it would not be the same as the file path above. It might be something like common/myFile.jsp
or maybe even common/myFile
.
If this were the case, then I must use the publicly accessible URL for an AJAX request to be able to sent to that URL.
It looks like your URL is incomplete. The most likely cause for the error is that you dont have a fully qualified URL.
Try specifying the full path and see if that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With