Site icon NgDeveloper

Ajax Calling in jQuery

Step 1:  Callingajax.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Javadomain example</title>
</head>
<script src="scripts/jquery-1.9.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$('.button').click(function() {
			$.ajax({
				url : "sample.html",
				type : "GET",
				error : function() {
					alert("failure");
				},
				success : function(data) {
					$('#cm').html(data); 
// to display the sample.html file content.
				}
			});
		});
	});
</script>
<body>
	<h1 align="center">Ajax Call Using Jquery</h1>
	<div class="content">
		<br />
		<div class="button" id="cm" align="center">click me</div>
		<br />
	</div>
</body>
</html>

 

Step 2: sample.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome to Javadomain.in
</body>
</html>

 

Execute the Callingajax.html file and you will have the button, if you click that button, content from the sample.html file will be displayed.

Note: Don’t forget to add the required sources (jquery-1.9.0.min.js).

Download Here: AjaxInsideJquery 

Exit mobile version