如何在另一个函数中调用Ajax函数的回调

时间:2016-09-06 09:25:48

标签: javascript jquery ajax

我有一个Ajax函数,它从Web服务加载数据。它看起来像这样:

public static void Main()
{  
   TcpListener server=null;   
   try
   {
  // Set the TcpListener on port 13000.
  Int32 port = 13000;
  IPAddress localAddr = IPAddress.Parse("127.0.0.1");

  // TcpListener server = new TcpListener(port);
  server = new TcpListener(localAddr, port);

  // Start listening for client requests.
  server.Start();

  // Buffer for reading data
  Byte[] bytes = new Byte[256];
  String data = null;

  // Enter the listening loop.
  while(true) 
  {
    Console.Write("Waiting for a connection... ");

    // Perform a blocking call to accept requests.
    // You could also user server.AcceptSocket() here.
    TcpClient client = server.AcceptTcpClient();            
    Console.WriteLine("Connected!");

    data = null;

    // Get a stream object for reading and writing
    NetworkStream stream = client.GetStream();

    int i;

    // Loop to receive all the data sent by the client.
    while((i = stream.Read(bytes, 0, bytes.Length))!=0) 
    {   
      // Translate data bytes to a ASCII string.
      data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
      Console.WriteLine("Received: {0}", data);

      // Process the data sent by the client.
      data = data.ToUpper();

      byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

      // Send back a response.
      stream.Write(msg, 0, msg.Length);
      Console.WriteLine("Sent: {0}", data);            
    }

    // Shutdown and end connection
    client.Close();
  }
}
catch(SocketException e)
{
  Console.WriteLine("SocketException: {0}", e);
}
finally
{
   // Stop listening for new clients.
   server.Stop();
}


Console.WriteLine("\nHit enter to continue...");
Console.Read();
 }   

我需要对此Web服务在另一个文件中的另一个函数的响应结果进行一些处理。

我的文件包含在我的ajax函数中发送数据的功能。看起来像这样

 $.ajax({
    type: 'POST',
    url: back + this.path_Uri,
    data: {msg:this.msg},
    dataType: "json",
    success: function (data, statut) {
                        // traitement to not execute here
        window.reloadFreelancersMission= data;
        var number = reloadFreelancersMission.pagination[0].pagenbr;
        window.pagination.ReloadPager(number);
        /////////////////////////////////////////////////
    },
    error: function (data) {
        console.log(data);
    }
});

所以我需要在这里以这种方式使用我的函数的回调:

sendFiltres: function () {
    $("body").on('change', '.filtres' , function () {
        var choix_filtres = $("#fitre_recherche");
        var msgJson = JSON.stringify(serializeObject($("#fitre_recherche")));
        window.sendData.start(choix_filtres,msgJson);
    })
},

并整合我的治疗,以获得这个:

request.done(function( msg ) {
    console.log( msg );
}); 

但似乎我无法在此文件中调用此回调。我只允许在我的ajax函数的同一个文件中使用它,而这正是我的问题。

1 个答案:

答案 0 :(得分:0)

就我而言,我通过在jQuery ajaxSuccess的回调函数中恢复服务的响应来解决我的问题

$(document).ajaxSuccess(function (event, xhr, settings) {
    if (settings.url === window.find_mission.route.getmission || settings.url === window.find_freelance.route.getfreelance) {
        xhr = xhr.responseJSON;
        var pagenumber = xhr.pagination[0].pagenbr;
        window.pagination.ReloadPager(pagenumber);
     }
 });

我使用xhr变量来获取数据。