user816604
user816604

Reputation:

reload contents of a page using ajax without refresh

i want to reload the contents of a page using ajax without actually refreshing the page. so using ajax, ALL of contents of the page changes without actually reloading the page. How can I do that?

I can't use a div because I want to replace the whole page (and thus want to be w3c html validating compliant).

how can I do this using jquery and ajax?

Upvotes: 0

Views: 3131

Answers (1)

Fraz0r
Fraz0r

Reputation: 396

AJAX came about for scenarios where updating an entire page wasn't resource effective (i.e. when only part of the page is going to be updated after a request). For your example, a page refresh sounds ideal - since all content is going to be changed anyway.

AJAX isn't the best answer in every situation, but if you are adamant to solving your problem with AJAX...

Using jQuery:

$.post('/login', {login: 'username', password: 'password'}, function(response) { $('body').html(response); }, 'text');

Note, this only updates the body - so the response shouldn't include html, head, body, etc.

A better solution would be to assign sections to parts of your page, addressable by AJAX updates. Your AJAX responders could then return an associative array containing the ID of these sections as the keys, and their contents as the values. A simple loop could then update any (or all) parts of the page with little modification to the snippet I provided above.

Upvotes: 3

Related Questions