Real-time load result (ajax)
I dont know how write this script. I need load results with some URL (eg: localhost/index.php?action=get&type=29) and this result to give a variable for furt开发者_JS百科her processing. I need create this on Jquery. Thanks.
There are features in jQuery that can be used, you have either a quick load using the simple .load()
function that will parse the HTML into an element. Usage:
$('#element').load('http://www.urlgoeshere.com/');
With processing events with AJAX with further writing options, you have many options to choose from:
- http://api.jquery.com/jQuery.ajax/
These options below are using the .ajax()
function but makes the writing easier:
- http://api.jquery.com/jQuery.get/
- http://api.jquery.com/jQuery.post/
EDIT: With your "refresh", you can use the setTimeout
function to repeat the ajax call:
setTimeout(function()
{
$.ajax({
type: "POST",
url: "some.php",
data: "action=get&type=29",
success: function(arg){
// do something
}
});
}, 1000); // This will "refresh" every 1 second
$.ajax({
type: "POST",
url: "some.php",
data: "action=get&type=29",
success: function(arg){
// do something
}
});
you can use
$.ajax({
url: "localhost/index.php",
data: "action=get&type=29",
success: function(data, status, xhr){
// data is the response from the server, status is the http status code,
// xhr is the actual xhr
},
error: function(){
},
...
});
to get the results.
Be sure to define the success
and error
callbacks to process the data when it is returned.
Look here http://api.jquery.com/jQuery.ajax/ to get started.
精彩评论