jQuery还是JS:回帖后的Piggyback?

时间:2015-07-11 00:16:06

标签: javascript jquery

我正在寻找一种在POST Ajax调用完成时触发事件的方法,并对来自任何脚本/插件外部的返回数据执行某些操作,而不修改任何POST回调代码。请参阅伪代码:

$.post(url, {id: 3,...}, function( d ) {
    ...some js code here
});

//Looking for an event like this :
$(document).on('post', '??' ,function() {
    //I want access to 'd' from post callback in here, and any other POST calls return data...
}

2 个答案:

答案 0 :(得分:2)

尝试使用.ajaxComplete()

$(document).ajaxComplete(function(event, jqxhr, settings) {
  // I want access to 'd' from post callback in here, 
  // and any other POST calls return data...

  // do stuff when `$.post()` completes
  console.log(jqxhr.responseText); // `d`
});

$.post(url, {id: 3,...}, function( d ) {
 // do stuff
 // ...some js code here
});

答案 1 :(得分:0)

不确定为什么不使用import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; import java.util.List; public class MainActivity extends Activity { private String user = "user"; private String pass = "pass"; private String host = "hostname"; private int portNum = 22; private String fileName = "sample.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new AsyncTask<Void, Void, List<String>>() { @Override protected List<String> doInBackground(Void... params) { try { Downloader(fileName); } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(); } public void Downloader(String fileName) { JSch jsch = new JSch(); Session session = null; String knownHostsDir = "/home/user/"; try { jsch.setKnownHosts(knownHostsDir); } catch (JSchException e) { e.printStackTrace(); } try { session = jsch.getSession(user, host, portNum); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(pass); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.get(fileName); Log.d(fileName, " has been downloaded"); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } } success:的{​​{1}}属性。

回答你的实际问题:在$ .post的回调中:

$.post

访问:

.done(function() {})

了解更多信息:https://learn.jquery.com/events/introduction-to-custom-events/