In my project I have a column with several filters with jQuery Ajax Calls in order to reduce number of poducts shown in main.php <div id="target">
. Works fine, but after eg sorting the results or clicking pagination in response file main.php, I need to be able to do a new filter-action in the filter-column with the $_GET parameters of the last Ajax call of main.php
. These paramaters show up fine in Firebug tab Net - tab XHR - tab Parameters.
But I can't find a way to retrieve the parameters and put them in my function which fires the Ajax call:
function updateStatus(content_show, data) {
jQuery.ajax({
method: "get",
url: content_show,
data: data,
beforeSend: function(){
jQuery("#target").html('<p><img src="images/ajax_load.gif" /></p>');
}, //show loading just when link is clicked
success: function(html) {
// update status element
jQuery('#target').show("slow");
jQuery('#target').html(html);
}
});
}
In my filter file I have a function to check and uncheck the checkboxes and start the ajax call:
function check_them(obj,URL) {
var getstr = "";
var man_id_selected ="";
for (i=0; i<obj.getElementsByTagName("input").length; i++) {
if (obj.getElementsByTagName("input")[i].type == "checkbox") {
if (obj.getElementsByTagName("input")[i].checked) {
getstr = "" + obj.getElementsByTagName("input")[i].name + "=";
man_id_selected += obj.getElementsByTagName("input")[i].value + "_";
} else {
<!-- getstr += obj.getElementsByTagName("input")[i].name + "=&";-->
}
}
}
getUrlStatus('main.php', function(status) { // try to find $_GET parameters of main.php
alert(status);
});
updateStatus(URL, getstr+man_id_selected);
}
Here I also tried to find the XHR parameters with:
function getUrlStatus(url, callback) {
jQuery.ajax({
url: url,
complete: function(xhr) {
callback(xhr.status);
}
});
}
But the latter off course will only return Status 200 and not the Parameters.
Edit
After more reading and testing I almost solved the problem by changing my original PHP code and functions for proper formatting of the function updateStatus with a callback:
function updateStatus(url, base_url, new_parameters, old开发者_JAVA技巧_parameters) {
jQuery.ajax({
method: 'get',
url: base_url,
data: new_parameters,
beforeSend: function(){
jQuery("#target").html('<p><img src="images/ajax_load.gif" /></p>');
}, //show loading just when link is clicked
complete: function(){
jQuery("#target").hide("fast");
}, //stop showing loading when the process is complete
success: function(html) {
// update status element
jQuery('#target').show("slow");
jQuery('#target').html(html);
callback(this.url)
}
});
}
Now I only have I slight problem retriving and calling the new url to $url in PHP. I will elaborate on that and possibly post a new topic.
For future reference, if you're using jQuery 1.4+, the success function actually accepts 3 arguments, it's just that the second two are optional and data
is usually the only one ever needed.
success(data, textStatus, XMLHttpRequest):Function
A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the 'dataType' parameter; a string describing the status; and the XMLHttpRequest object (available as of jQuery 1.4). This is an Ajax Event.
So next time if you need to get more information about the XHR response, you can set up your success function like so:
success: function(data, status, xhr) {
// tests with status and xhr, etc...
}
精彩评论