5

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.

James
  • 2,876
  • 18
  • 72
  • 116

1 Answers1

4

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".

Shawn Wernig
  • 1,742
  • 14
  • 18
  • Shawn, thank you for your reply. I'm using the jQuery Form Plugin [link](http://www.malsup.com/jquery/form/). I think I could apply your closure idea and get this to work with the form plugin. The more that I think about this though, it seems that it probably is best for the server to echo the params. After all, it is building the response based on those params. I welcome your or others thoughts but that is the way I seem to be leaning now. Thanks again for your answer. – James Oct 08 '12 at 14:04