Skip to main content

Update whole page on Ajax request



I have an AJAX request that can have two possible outcomes:





  1. The server responds with a message which I should place in a <div>



  2. The server responds with an HTML page, in this case I need to substitute current page with a new one and change the address (the client knows the address before a request).







What would be the solution if I have the AJAX request that needs to handle both of these cases?







url = "http://example.com"

ajax.request(callback)



function callback(response) {

if (case2(response)) {

history.pushState({}, "New page", url);

document.innerHTML = response

} else {

updateDiv(response)

}

}







I'm interested in a correct way to implement the first branch, or if the server can somehow compose a headers that will make browser to handle a response as a usual HTTP response and update a page location and content, something like redirect with given content.





I understand that the server can return a link instead of a page, but in this case one additional stage will be needed on a client - redirect and then populating the new page on the server.


Comments

  1. Quite frankly, I think that approach is basically broken by design. You shouldn't have to make that decision at that place. For example, the ajax response could only signal that a whole new page should be loaded and the new content then be generated on a second (non-ajax) request to a new URL.

    In case you're forced to take the way you already go, and provided the response content is not very large, you could try Javascript-URIs. Basically, an URI in the form of javascript:"string" will load a new page which that string is the source code for. So, if response already is a string, just assigning javascript:response to window.location.href should suffice. Maybe you have to do some escaping beforehand. And I don't know, how cross-browser-compatible this approach is.

    <a href="javascript:response">load</a>


    is also possible.

    A variant of this is building the URL not with the variable name, but with the actual string data. Like

    function source2url(src) {
    // make valid javascript string from source text
    var esc1 = src
    .replace(/\\/g, '\\\\')
    .replace(/\'/g, '\\\'')
    .replace(/\x0A/g, '\\x0A')
    .replace(/\x0D/g, '\\x0D');

    // make valid url from that
    return "javascript:'" + encodeURIComponent(esc1) + "'";
    }

    window.location.href = source2url(response);


    This will, of course, generate pretty large URIs. And you'll always have the Javascript-URI in the address bar.

    UPDATE

    A similar approach is to use base64 encoding in a data URI. The Wikipedia entry explains how it works, including a javascript example. However, you'd have to base64-encode the content somehow. (Note: You can use data URIs with or without the base64 encoding. You have to see what gives you shorter URIs for your specific content.)

    ReplyDelete
  2. I had a similar issue once. A full error page was returned instead of a simple HTML snippet. We eventually fixed this by changing the logic, but here is one of the solutions I found:

    document.open();
    document.write(responseText);
    document.close();


    The reason we abandoned this is that on IE there were some problems. I didn't loose any time to investigate why, but it threw an 'Access denied' exception when attempting to write the string. I think there were some <meta> tags that confused IE, or maybe conditional comments, I'm not sure. (It worked when I used some simple pages...)

    Bottom line is: you shouldn't have to do this, but if there is nothing else you can do (like returning an url string) the code above might work.

    ReplyDelete
  3. It's really easy if the response is valid XML.

    var new_doc = (new DOMParser).parseFromString(response, "application/xml");
    document.replaceChild(document.adoptNode(new_doc.doctype), document.doctype);
    document.replaceChild(document.adoptNode(new_doc.documentElement), document.documentElement);

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...