简体   繁体   English

在jquery $ .post或等效的PHP中获取响应

[英]Getting response from PHP in jquery $.post or equivalent

I'm sending JQUERY $.post request to my php method, it looks like that: 我正在向我的php方法发送JQUERY $ .post请求,它看起来像这样:

$.post("/controller/method/",{id: cell_id},function(data,status,xhr)
{
         some_javascript_function();
})

PHP: PHP:

public function method()
{
   insert to database code...
}

my PHP function just insert a record into table, which some_javascript_function() will use - cause it request data from database. 我的PHP函数只是将一条记录插入表中,some_javascript_function()将使用它 - 导致它从数据库请求数据。 Sometimes my PHP function is too slow to insert record before some_js_function() tries to get it from database. 有时我的PHP函数在some_js_function()尝试从数据库中获取它之前插入记录太慢。

Hoe can I manage some response from PHP when everything is ready, and then call some_js_function? 当一切准备就绪时,我可以从PHP管理一些响应,然后调用some_js_function吗?

Start the read after the write has returned. 写入返回后开始读取。 $.post has a callback function, that's when you're guaranteed the write has completed. $.post有一个回调函数,当你保证写完成时。

What you have to do is return data from the PHP function after the insertion is done. 您需要做的是在插入完成后从PHP函数返回数据。 It would look like below. 它看起来像下面。

function store()
{
    //your code......

    //let's assume that store function will do the data storing part.

    $state = store(); // you should return true after the data insertion is successful

    if($state)
     echo $data; // here we echo data to used in the callback function.
}

Callback function is executed after the current effect is 100% finished. 当前效果100%完成后执行回调功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM