当前位置: 动力学知识库 > 问答 > 编程问答 >

php - multiple callback using jquery .post ajax

问题描述:

Is it possible to get multiple callbacks using jQuery .post for a ajax upload of an image to php?

I am sending an image to the server for processing thru binary string and would like to hav the front end display status when the image has uploaded then a processing icon.

Currently I am using the standard .post but ideally this is the scenario i am looking for -

jquery:

$.post( 'process.php', { myvar:myvar}, function(data){

if(data=='callback_1'){

// process 1

}else if(data=='callback_2'){

// process 2

}

});

php:

$myvar = $_POST['myvar'];

// after process 1

echo 'callback_1'

// after process 2

echo 'callback_2'

网友答案:

I set an array in the php script

$r['callback_one'] = 'complete';
$r['callback_two'] = 'fail';

echo json.encode($r);

javascript:

r = JSON.parse(data);
if (r.callback_one === 'complete){//do something};

of course this still sends the callbacks all at once, which may not be what you are after.


the only thing i can think of if you need to try and get things sequentially is to use flushing:

How to flush output after each `echo` call?

ob_end_flush();

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_start();

What may even be happening is that the client does not receive the packet from the server until the server has built up enough characters to send what it considers a packet worth sending.

Old Answer:

You could use ob_implicit_flush which will tell output buffering to turn off buffering for a while:

ob_implicit_flush(true);

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_implicit_flush(false);
分享给朋友:
您可能感兴趣的文章:
随机阅读: