jQuery - AJAX - Get Requested Parameters in success function

那年仲夏 提交于 2019-12-10 15:39:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!