javascript Jquery Pjax - Ajax 成功函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21679655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:28:01  来源:igfitidea点击:

Jquery Pjax - Ajax success function

javascriptjqueryajaxpjax

提问by even2be

Currently I am translating my ajax calls to regular $.pjax()call. Everything works fine but ajax success function. I can't manage how to call pjax success function with given parameters.

目前我正在将我的 ajax 调用转换为常规$.pjax()调用。一切正常,但 ajax 成功功能。我无法管理如何使用给定参数调用 pjax 成功函数。

The only thing I can use is defining pjax global success function to be called on each pjax call:

我唯一可以使用的是定义要在每次 pjax 调用时调用的 pjax 全局成功函数:

   $(document).on('pjax:success', function(event, data, status, xhr, options) {

   });

But unfortunately I would like to define per call specific success function.

但不幸的是,我想定义每个调用特定的成功函数。

Ajax call example:

Ajax 调用示例:

 $.ajax({
    url:"/myPage/myFunction",
    type:"POST",
    data:giveMeData(),
    success:function(data){$('#right_form').html(data);console.log('Success works!')}
 });

Pjax call example:

Pjax 调用示例:

 $.pjax({
    url:"/myPage/myFunction",
    type:"POST",
    container:'#right_form',
    data:giveMeData(),
    success:function(){console.log('Success works!')}
 });

采纳答案by Carl

I don't believe that the jQuery PJAX library supports passing a "success" function directly in to a $.pjax call, although I suspect you could work around this using the $(document).on('pjax:success')callback & its optionsattribute in order to achieve the same functionality.

我不相信 jQuery PJAX 库支持将“成功”函数直接传递给 $.pjax 调用,尽管我怀疑您可以使用$(document).on('pjax:success')回调及其options属性来解决此问题以实现相同的功能。

For example, say your request is like the above, but you want to have a custom success callback you could use something like this:

例如,假设您的请求与上述类似,但您想要自定义成功回调,您可以使用以下内容:

 $.pjax({
    url:"/myPage/myFunction",
    type:"POST",
    container:'#right_form',
    data:giveMeData(),
    custom_success:function(){console.log('Custom success works!')}
 });

Then, in order to run the custom_successmethod you could hook up the standard pjax success listener, and given that all the parameters provided to $.pjaxare made available in options, you can then grab custom_successfunction and run it. So your listener may look something like example

然后,为了运行该custom_success方法,您可以连接标准的 pjax 成功侦听器,并且假定提供给的所有参数$.pjax都在选项中可用,然后您可以获取custom_success函数并运行它。所以你的听众可能看起来像例子

 $('#right_form').on('pjax:success', function(event, data, status, xhr, options) {
      // run "custom_success" method passed to PJAX if it exists
      if(typeof options.custom_success === 'function'){
           options.custom_success();
      }
 });

Which i *think* would provide the sort of functionality your after?

我*认为*会提供你所追求的那种功能?

回答by Coz

A late answer but I found the solution here.

一个迟到的答案,但我在这里找到了解决方案。

$.pjax({
    url:"/myPage/myFunction",
    type:"POST",
    container:'#right_form',
    data:giveMeData(),        
 }).done(function() { console.log('Success works!') });