eve*_*2be 15 javascript ajax jquery pjax
目前我正在将我的ajax调用转换为常规$.pjax()调用.一切正常,但ajax成功功能.我无法管理如何使用给定参数调用pjax成功函数.
我唯一可以使用的是定义在每次pjax调用时调用的pjax全局成功函数:
   $(document).on('pjax:success', function(event, data, status, xhr, options) {
   });
但不幸的是,我想定义每个调用特定的成功函数.
Ajax调用示例:
 $.ajax({
    url:"/myPage/myFunction",
    type:"POST",
    data:giveMeData(),
    success:function(data){$('#right_form').html(data);console.log('Success works!')}
 });
Pjax调用示例:
 $.pjax({
    url:"/myPage/myFunction",
    type:"POST",
    container:'#right_form',
    data:giveMeData(),
    success:function(){console.log('Success works!')}
 });
Car*_*arl 12
我不相信jQuery PJAX库支持将"success"函数直接传递给$ .pjax调用,尽管我怀疑你可以使用$(document).on('pjax:success')callback及其options属性来解决这个问题,以实现相同的功能.
例如,假设您的请求与上述类似,但您希望自定义成功回调,您可以使用以下内容:
 $.pjax({
    url:"/myPage/myFunction",
    type:"POST",
    container:'#right_form',
    data:giveMeData(),
    custom_success:function(){console.log('Custom success works!')}
 });
然后,为了运行该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();
      }
 });
我认为*会提供您之后的功能吗?
迟到的答案,但我在这里找到了解决方案.
$.pjax({
    url:"/myPage/myFunction",
    type:"POST",
    container:'#right_form',
    data:giveMeData(),        
 }).done(function() { console.log('Success works!') });
| 归档时间: | 
 | 
| 查看次数: | 9772 次 | 
| 最近记录: |