jQuery的 - AJAX - 在成功函数获取请求的参数 [英] jQuery - AJAX - Get Requested Parameters in success function

查看:673
本文介绍了jQuery的 - AJAX - 在成功函数获取请求的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能取得成功(回调)函数所请求的参数?

Is it possible to get the requested parameters in the success (callback) function?

例如,

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
}

当然,

,我可以存储在一个全局变量的请求参数handleSubmitClick期间和阅读loadSearch变量。然而,如果用户提交另一个请求之前loadSearch从第一请求执行该变量的值将被覆盖(我不想prevent从发出另一个请求,直到第一完成处理的用户)。

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.

推荐答案

这可能是你在找什么,但它使用比当前解决方案是不同的AJAX功能:

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" );

$(本).attr(数据)将有原来的请求参数,你可以把它当作一个对象,所以

$(this).attr('data') would have the original request params, and you can treat it like an object, so

$(this).attr('data')->dataType

将是JSON。

would be "json".

这篇关于jQuery的 - AJAX - 在成功函数获取请求的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆