Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getJSON Synchronous

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).

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);          });     }  }); 

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 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.
like image 741
Fred Avatar asked Oct 22 '12 10:10

Fred


People also ask

What does getJSON mean?

The getJSON() method is used to get JSON data using an AJAX HTTP GET request.

What are the arguments of getJSON method?

It is a callback function that executes on the successful server request. It also has three parameters that are data, status, and xhr in which data contains the data returned from the server, status represents the request status like "success", "error", etc., and the xhr contains the XMLHttpRequest object.

What is the difference between getJSON and Ajax in jQuery?

getJSON() is equal to $. ajax() with dataType set to "json", which means that if something different than JSON is returned, you end up with a parse error. So you were mostly right about the two being pretty much the same :).

How jQuery read data from JSON file?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.


2 Answers

Since $.getJSON() uses ajax configurations, just set the global ajax configs:

// 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 }); 
like image 115
cchristelis Avatar answered Sep 18 '22 13:09

cchristelis


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,     ... }) 
like image 37
tobspr Avatar answered Sep 20 '22 13:09

tobspr