On Fri, 14 Mar 2014 18:28:58 GMT, Dennis R. Arriola wrote:

On Mon, 03 Feb 2014 08:44:58 GMT, Sönke Ludwig wrote:

(...)
The most simple way to solve this would be to explicitly add a route for this in the URLRouter:

void sendOptions(HTTPServerRequest req, HTTPServerResponse res)
{
    res.headers["Access-Control-Allow-Origin"] = "*";
    res.writeBody("");
}

auto router = new URLRouter;
router.match(HTTPMethod.options, "/register", &sendOptions);
// ...

Plus probably the header should also be sent with every other response:

void addACAO(HTTPServerRequest req, HTTPServerResponse res)
{
    res.headers["Access-Control-Allow-Origin"] = "*";
}

void sendOptions(HTTPServerRequest req, HTTPServerResponse res)
{
    addACAO(req, res);
    res.writeBody("");
}

auto router = new URLRouter;
router.any("*", &addACAO);
router.match(HTTPMethod.options, "*", &sendOptions);
// ...

The "*" for the header value should also probably be something more specific for the application.

Currently there is not really a sane way to implement this directly in vibe.d. But later, when the high level web framework in vibe.web gets some more features, that would be a good place to add something like this by default.

Hi! Where can I add the code you've given. I'm confused. Im using angularJS and got the same error. I hope to get a response on this. Thanks

Sorry that the answer took so long, I've been very busy with non-computer-related things recently.

If your project is organized similarly to the example in the first steps section, you'd insert the any and match method calls right after the URLRouter has been created. All other routes go after those calls. This will make sure that the router first calls addACAO to add the header, before falling through to the regular routes (it falls through because addACAO doesn't write a respnse). addACAO and sendOptions can basically be defined anywhere, but probably somewhere down in the same module.

For the "first steps" example, it would look like this:

import vibe.d;

shared static this()
{
	auto router = new URLRouter;
	router.any("*", &addACAO);
	router.match(HTTPMethod.options, "*", &sendOptions);
	router.get("/", &index);
	// add other routes here

	auto settings = new HTTPServerSettings;
	settings.port = 8080;

	listenHTTP(settings, router);
}

void addACAO(HTTPServerRequest req, HTTPServerResponse res)
{
	res.headers["Access-Control-Allow-Origin"] = "*";
}

void sendOptions(HTTPServerRequest req, HTTPServerResponse res)
{
	addACAO(req, res);
	res.writeBody("");
}

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