问题
Is it possible to get the requested parameters in the success (callback) function?
For example,
function handleSubmitClick(evt) {
evt.preventDefault();
var setupOptions = {
success: loadSearch,
type: "POST",
dataType: "json",
url: "../search.x",
error: handleError,
timeout: 10000
};
$("#searchForm").ajaxSubmit(setupOptions);
return false;
}
function loadSearch(data, statusText, xhr, $form) {
// here is where I would like to get the HTTP request
// parameters
}
Of course, I could store the request params in a global variable during handleSubmitClick and read the variable in loadSearch. However, the value of that variable will be overridden if the user submits another request prior to loadSearch executing from the first request (and I do not want to prevent the user from issuing another request until the first completes processing).
I currently have the server echo the request params in the response data, but I'm hoping the request params is already available to the client without the server having to echo the request params. Thanks.
回答1:
This may be what you are looking for, though it uses a different AJAX function than what your current solution is:
$.post( your_ajax_request_url,
{
success: loadSearch,
type: "POST",
dataType: "json",
url: "../search.x",
error: handleError,
timeout: 10000
}, function( response ){
console.log( $(this).attr('data') );
}, "json" );
$(this).attr('data') would have the original request params, and you can treat it like an object, so
$(this).attr('data')->dataType
would be "json".
来源:https://stackoverflow.com/questions/12749630/jquery-ajax-get-requested-parameters-in-success-function