dongtang5776 2011-09-09 00:43
浏览 28
已采纳

POST后正在写入数据并调用不同步

I'm calling a view after an AJAX POST action. But I'm loading the view and calling Database data out of sync with the data being written. When I load the page, I look into the database and see the data there but it's not being rendered on the page. (I tried sessions and got the same problem). In order for the view to actually render the data from the database, I need to actually refresh the page. Same thing with the sessions, I need to refresh the page in order for the sessions data to render. Is there a common reference to this kind of issue? I read about POSTBACK, is this something I need to look into further? It's basically a strange sync issue where the data is not being generated until I refresh the page and re-load it.

  • 写回答

1条回答 默认 最新

  • douzinei6926 2011-09-09 02:14
    关注

    jQuery ajax call are async, by default. The javascript code continue without waiting for the return from the server.

    So let look at :

     $.post('source.php',function(valueFromPHP){
        alert(valueFromPHP)
     });
     alert('Hello world')
    

    In the previous example Hello Wold is alerted first, then the value from php.

    If you want to insert the return value from php into your page you need to place all your code into the sucess function :

     $.post('source.php',function(valueFromPHP){
        alert(valueFromPHP)
        alert('Hello world')
     });
    

    In the second example the value return from PHP is alerted first, then Hello world

    So if you want to render the data received from php make sure you place your code inside the success function. Normally that is where we get error while working with jQuery ajax.

    Let me know if it help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?