jQuery getJSON 同步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13009755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getJSON Synchronous
提问by Fred
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).
目标:我所追求的是每次在数据库中添加内容时(在 $.ajax 到 submit_to_db.php 之后)从数据库中获取数据并刷新 main.php(通过 draw_polygon 更明显)。
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.
所以基本上我有一个 main.php,它将 ajax 调用另一个 php 来接收一个将保存到数据库的数组,一个 json 调用另一个 php 来返回一个数组,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);
});
}
});
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.
就我而言,我所做的是一个get_from_db
函数调用,用于getJSON
从数据库中实际获取数据,并将数据用于draw_polygon
. 但这是应该这样做的吗?我是一个完全的新手,getJSON
老实说,这是我第一次尝试使用ajax。所以我的问题是:异步实际上是如何工作的?是否有其他的办法解决这个,而不必调用函数get_from_db
与getJSON
(这是不同步的,是它那为什么它时,它不是一个函数内没有更新页面?)所有的时间-就像$.ajax
用async: false
(顺便说一下,我无法让它工作)。我的方法奏效了,但我想也许还有其他更好的方法可以做到。我很想学习如何。提前致谢。我希望我说得有道理。
To make it more clearer, here's what I want to achieve:
为了更清楚,这是我想要实现的目标:
@start
of page, get data from database (currently throughgetJSON
)- Paint or draw in
canvas
using thedata
- When I click the done button it will update the database
- I want to AUTOMATICALLY get the data again to repaint the changes in canvas.
@start
的页面,从数据库中获取数据(当前通过getJSON
)- 油漆或绘制在
canvas
使用data
- 当我单击完成按钮时,它将更新数据库
- 我想再次自动获取数据以重新绘制画布中的更改。
采纳答案by tobspr
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,
So for example:
例如:
$.ajax({
url: "myurl",
async: false,
...
})
回答by cchristelis
Since $.getJSON() uses ajax configurations, just set the global ajax configs:
由于 $.getJSON() 使用 ajax 配置,只需设置全局 ajax 配置:
// Set the global configs to synchronous
$.ajaxSetup({
async: false
});
// Your $.getJSON() request is now synchronous...
// Set the global configs back to asynchronous
$.ajaxSetup({
async: true
});
回答by Robert Moskal
$.getJSON(), doesn't accept a configuration, as it says in the docs it's a shorthand version of:
$.getJSON() 不接受配置,因为它在文档中说它是以下的简写版本:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
So just rewrite your request in terms of that and async:falsewill work just as you expect.
因此,只需根据此重写您的请求,async:false就会按您的预期工作。
回答by R. Oosterholt
$.getJSON()
is a shorthand notation for $.ajax()
which can be configured to be synchronous (see jQuery.getJSONand JQuery.ajax):
$.getJSON()
是$.ajax()
可以配置为同步的速记符号(参见jQuery.getJSON和JQuery.ajax):
$.ajax({
dataType: "json",
url: url,
data: data,
async: false,
success: function(data) {
...
draw_polygon(data);
}
});
Try to avoid synchronous callsthough. Quote from jQuery doc (see async prop):
尽量避免同步调用。来自jQuery 文档的引用(请参阅 async 道具):
Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.
跨域请求和 dataType: "jsonp" 请求不支持同步操作。请注意,同步请求可能会暂时锁定浏览器,从而在请求处于活动状态时禁用任何操作。
You might want to try jQuery Deferredslike this:
你可能想像这样尝试 jQuery Deferreds:
var jqxhr = $.getJSON(url);
jqxhr.done(function(data) {
...
draw_polygon(data);
});