RejectedSoftware Forums

Sign up

page wont render

Hi,

I cant seem to get a page (hello.dt) to render when the request comes via an ajax hijacked redirect (doSomething()). The hello() handler does get called, so am I missing something, I wonder?

The hello.dt page renders fine with a regular GET.

Please see below, thanks.

app.d:

import vibe.d;

shared static this()
{
auto router = new URLRouter;

    router.get("/", &index)
	  .get("/hello/:greeting", &hello)
	  .post("/doSomething", &doSomething)
	  .get("*", serveStaticFiles("./public/"));

    auto settings = new HTTPServerSettings;
    settings.port = 8080;
    settings.bindAddresses = ["::1", "127.0.0.1"];

    listenHTTP(settings, router);
}

void index(HTTPServerRequest req, HTTPServerResponse res)
{
    res.render!("index.dt");
}

void doSomething(HTTPServerRequest req, HTTPServerResponse res)
{
    auto hello = req.form["hello"];
    logInfo("in doSomething: " ~ hello);
    res.redirect("/hello/" ~ hello);
}

void hello(HTTPServerRequest req, HTTPServerResponse res)
{
    auto hello = req.params["greeting"];
    logInfo("in hello: " ~ hello);
    res.render!("hello.dt", hello);
}

index.d:

extends layout

block body
    p
        a(id="hello", href="/hijackme") hijacked hello
    p
        a(href="/hello/hello-world") hello

hello.dt:

extends layout

block body
    h1 #{hello}

hijack.js (jQuery):

$(document).ready(function() {
    hijacks.hello();
});

hijacks = {};

hijacks.hello = function() {
    $('#hello').on('click', function(e) {
        alert("hijacking hello");
		e.preventDefault();

        $.post('doSomething', {'hello':'hijacked-hello-world'}, function () {
            alert("ajax success");
        });
    });
}

Re: page wont render

On Mon, 04 Aug 2014 17:44:37 GMT, james williams wrote:

Hi,

I cant seem to get a page (hello.dt) to render when the request comes via an ajax hijacked redirect (doSomething()). The hello() handler does get called, so am I missing something, I wonder?

The hello.dt page renders fine with a regular GET.

(...)

Redirect responses for XHR requests don't generally seem to be handled. A related stackoverflow topic suggests to add the redirect information in the response body in addition to the redirect header.

Re: page wont render

Thanks for the hint Sönke, this seems to work fine now.