POST 后数据写入和调用不同步

发布于 2024-12-03 13:37:02 字数 241 浏览 0 评论 0原文

我在 AJAX POST 操作后调用视图。但我加载视图并调用数据库数据与正在写入的数据不同步。当我加载页面时,我会查看数据库并查看那里的数据,但它没有呈现在页面上。 (我尝试了会话并遇到了同样的问题)。为了使视图实际呈现数据库中的数据,我需要实际刷新页面。与会话相同,我需要刷新页面才能呈现会话数据。对于此类问题有共同的参考吗?我读到有关 POSTBACK 的内容,这是我需要进一步研究的内容吗?这基本上是一个奇怪的同步问题,在我刷新页面并重新加载之前,不会生成数据。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2024-12-10 13:37:02

默认情况下,jQuery ajax 调用是异步的。 javascript 代码继续执行,无需等待服务器返回。

让我们看一下:

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

在前面的示例中,首先警告 Hello Wold,然后是来自 php.ini 的值。

如果您想将 php 的返回值插入到您的页面中,您需要将所有代码放入 sucess 函数中:

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

在第二个示例中,首先警告 PHP 返回的值,然后 Hello world

因此,如果如果您想要渲染从 php 接收到的数据,请确保将代码放在 success 函数中。通常这就是我们在使用 jQuery ajax 时遇到错误的地方。

如果有帮助请告诉我!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文