问题
$.ajax({
type: "GET",
data: "id="+id+"&id-other="+id-other,
url: "ajax1.php"
}).done(function(data){
$("#div").html(data);
});
I have the code piece above, I search the web but I don't know how to explain what it is for. Is there any tutorials of ajax basics explaining step by step of what$.ajax()
means, what type:Get
does, what data:...
does etc ?
回答1:
It is making an ajax (asynchronous) call to a remote page.
type: get
It is a HTTP Get request. The form data will be encoded in the url as query string values.
data: "id="+id+"&id-other="+id-other
This the data being passed to the server page
url: "ajax1.php"
ajax1.php is the server page which handles the ajax request and reponds back,
.done(function(data){
$("#div").html(data);
})
The code which inside the done event will be executed once the ajax call is completed. In this case we will get the response from the ajax call to a variable called data. We are setting that as the innerhtml of some HTML element with an id div.
read this link for more info : http://api.jquery.com/jQuery.ajax/
回答2:
$.ajax({
type: "GET",
data: "id="+id+"&id-other="+id-other,
url: "ajax1.php"
}).done(function(data){
$("#div").html(data);
Its really simple, we start by declaring AJAX function, then we declare the method(get or post - just like html form), data
is used the parameters to be passed through URL. URL
is the file which is invoked(just like action in forms). This will invoke your ajax1.php file and return some data, that data is returned in the success function or done function. In your case the data
is the data returned from your php file.
来源:https://stackoverflow.com/questions/10360304/ajax-basics-for-beginners