ASP+AJAX(Jquery) combination usage example
The editor of Downcodes today brings you a very simple example of using ASP+AJAX (Jquery), which is especially suitable for beginners to learn and use.
Sample code
The following examples show four commonly used AJAX request methods, namely:
1. $.ajax()
2. $.post()
3. $.get()
4. $.getJSON()
1. $.ajax()
`html
$(document).ready(function(){
$("#btn1").click(function(){
$.ajax({
url: "ajax.asp",
type: "POST",
data: {name: "Downcodes", city: "北京"},
success: function(result){
$("#div1").html(result);
}
});
});
});
`
2. $.post()
`html
$(document).ready(function(){
$("#btn2").click(function(){
$.post("ajax.asp", {name: "Downcodes", city: "北京"}, function(result){
$("#div2").html(result);
});
});
});
`
3. $.get()
`html
$(document).ready(function(){
$("#btn3").click(function(){
$.get("ajax.asp", {name: "Downcodes", city: "北京"}, function(result){
$("#div3").html(result);
});
});
});
`
4. $.getJSON()
`html
$(document).ready(function(){
$("#btn4").click(function(){
$.getJSON("ajax.asp", {name: "Downcodes", city: "北京"}, function(result){
$("#div4").html("姓名:" + result.name + "
城市:" + result.city);
});
});
});
`
ASP code sample (ajax.asp)
`asp
<%
Response.ContentType = "text/html; charset=utf-8"
Dim name, city
name = Request.Form("name")
city = Request.Form("city")
' Output JSON format data
Response.Write("{""name"": """ & name & """, ""city"": """ & city & """}")
%>
`
illustrate
The ajax.asp file in the above code needs to be placed in the same directory as the HTML file.
The $.ajax() function is the core of AJAX requests and can customize various request parameters.
The $.post() and $.get() functions are simplified ways of AJAX requests, corresponding to POST and GET requests respectively.
The $.getJSON() function is used to obtain data in JSON format.
In the ASP code example, Request.Form is used to obtain the parameters passed by the AJAX request.
Through this example, you should be able to understand how to use ASP with AJAX (Jquery) and use different AJAX request methods to achieve data interaction.
Summarize
The combined use of ASP+AJAX (Jquery) can make the website more dynamic and provide users with a better interactive experience. I hope this example can help you better understand and use ASP+AJAX (Jquery).