Ajax code without a web server
I am running ajax without a server on my system, I created one index.html with this
javascript function:
function do_the_click(url)
{
alert('inside this method do_the_click');
$.ajax({
async: false,
url : url,
contentType: "text/html",
type : "GET",
dataType : "text/html",
success: function(data){
alert('data');
}});
}
and my content is HTML content:
<a href="Java Collections.html" class="button" id="javacollections" onclick="do_the_click('this.href');"/>Java Collections</a>
I can get this message in the alert box * inside this do_the_click * method, but I couldn't get the data in the alert box, and so I couldn't get the Java Collections.html page in index.html.
I have searched a lot on google that how to load data locally without using a server in jquery.ajax but doesn't get any good solution, so I think it's okay for others if it's on stackoverflow.
source to share
I can run ajax without a webserver, the problem was my code below which I was using on my filesystem and you can use that as well.
function do_the_click(brl)
{
alert('inside this method do_the_click');
alert(brl);
var request = $.ajax({
async: false,
url: brl,
contentType: "text/html",
type : "GET",
dataType : "html",
});
request.done(function( msg ) {
alert(msg);
$( "#contentarea" ).load( msg, function() {
alert( "Load was performed." );
});
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
}
Java Collections
source to share
When you make AJAX requests using standalone html files, the HTTP request to the url is generated by the browser. All you need to do is make sure you have JQuery enabled. Here's what you need to do.
Include JQuery:
<script src="js/jquery-1.10.2.min.js"></script>
JQuery is located here in js folder. This is included in the standalone html file. Now, to make an AJAX request, use the following code.
$.ajax({
type: 'GET',
url: url,
success: function(data){
console.log(data);
},
async:false
});
source to share
Now, in the current scenario, it depends on where your server is located. If the server is somewhere else, it will require a CORS request (cross lookup request). So using file: // works fine with CORS.
I came to this question because I was looking for a reason why I can make an ajax request without using a web server. Got an answer Thank you!
source to share