getJSON 同步

2022-01-22 00:00:00 sync jquery ajax synchronous getjson

目标:我所追求的是每次在数据库中添加某些内容时(在 $.ajax 到 submit_to_db.php 之后)从数据库中获取数据并刷新 main.php(通过 draw_polygon 更明显).

GOAL: What I'm after is to get data from database and refresh main.php (more evident through draw_polygon) every time something is added in database (after $.ajax to submit_to_db.php).

所以基本上我有一个 main.php,它将 ajax 调用另一个 php 来接收一个将保存到数据库的数组,而一个 json 调用另一个 php 来返回一个数组将被 main.php 使用.

So basically I have a main.php that will ajax call another php to receive an array that will be saved to database, and a json call another php to return an array will be used by main.php.

$(document).ready(function() {
    get_from_db();
    $('#button_cancel').click(function(){
       $.ajax({
          url: 'submit_to_db.php',
          type: 'POST',
          data: {list_item: selected_from_list},

          success: function(result){
             ...
             get_from_db();
          }
       });
    });
    function get_from_db(){
         $.getJSON('get_from_db.php', function(data) {
             ...
             draw_polygon(data);
         });
    }
 });

在我的例子中,我所做的是 get_from_db 函数调用 getJSON 以实际从数据库中获取数据,数据将用于 draw_polygon.但这是应该怎么做的吗?我是一个完整的新手,老实说,这是我第一次尝试 getJSON 和 ajax.所以我的问题是:异步实际上是如何工作的?是否有另一种解决方法,而不是必须使用 getJSON 调用函数 get_from_db (它不是同步的,是吗?这就是为什么它不更新页面时它不在函数内?)一直 - 就像 $.ajaxasync: false 一样(顺便说一句,我无法让它工作).我的方法很有效,但我想也许还有其他更好的方法可以做到这一点.我很想学习如何.提前致谢.我希望我有任何意义.

In my case, what I did was a get_from_db function call for getJSON to actually get data from database, with the data to be used to draw_polygon. But is that how it should be done? I'm a complete newbie and this is my first time to try getJSON and ajax too to be honest. So my question: How does asynchronous work actually? Is there another workaround for this instead of having to call function get_from_db with getJSON (it isn't synchronous, is it? is that why it doesn't update the page when it isn't within a function?) All the time - like $.ajax with async: false (I couldn't get it to work by the way). My approach is working, but I thought maybe there are other better ways to do it. I'd love to learn how. Thanks in advance. I hope I'm making any sense.

为了更清楚,这是我想要实现的目标:

To make it more clearer, here's what I want to achieve:

  1. @start页面,从数据库中获取数据(目前通过getJSON)
  2. 使用 data
  3. canvas 中绘画或绘图
  4. 当我点击完成按钮时,它将更新数据库
  5. 我想再次自动获取数据以重新绘制画布中的更改.
  1. @start of page, get data from database (currently through getJSON)
  2. Paint or draw in canvas using the data
  3. When I click the done button it will update the database
  4. I want to AUTOMATICALLY get the data again to repaint the changes in canvas.

推荐答案

异步确实意味着请求在后台运行,并在收到响应时回调您的函数.如果您想获得结果但允许在请求中使用您的应用程序,则此方法是最好的.如果您想直接响应,请查看同步请求.此请求将暂停脚本执行,直到它得到响应,并且用户在收到响应之前不能做任何事情.您可以通过以下方式切换它:

Asynchronusly does mean the Request is running in the background, and calls your function back when it got a response. This method is best if you want to have a result but allow to use your app within the request. If you want to have a direct response, take a look at a synchron request. this request will pause script execution until it got a response, and the user can not do anything until the response was recieved. You can toggle it via:

async: false,

例如:

$.ajax({
    url: "myurl",
    async: false,
    ...
})

相关文章