JQuery & PHP requests. What's more efficient?
As I learn more and more about JQ开发者_运维知识库uery & PHP requests, a couple things have occured to me.
I've designed a web page with a bunch of empty DIVs that are populate by Ajax calls during and after this page loads. So far, in order for this page to load, I make a total of 8 (.get()) calls using JQuery/Ajax to php pages, get the results and populate this divs.
- On initial web page load, is it wise to:
A. Make 8 Ajax calls to 8 different PHP pages or... B. Make 1 Ajax call to 1 PHP page that generates the content of all previous 8 together?
- I've decided to use Ajax so that once page loads, each future user action is made through an Ajax call and would not require all of these original calls to be made again which I think would save a lot of server resources, etc..
Can any of you give some pointers on light-weight and efficient developing guidelines?
You should try to minimize your AJAX calls on page load. In a perfect world, the only AJAX calls being made would be due to user interaction AFTER the document has loaded. This means you should code your backend in a way that assembles and displays the entire page content initially.
On a side note, it's a good idea to make AJAX sites backwards compatible. Something in your PHP like:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if(!IS_AJAX) { require_once("header.php"); }
echo "this is ajax content";
if(!IS_AJAX) { require_once("footerer.php"); }
This will detect whether or not it was an ajax request, and if not display the header and footer for normal operation.
精彩评论