This project is read-only.

How to properly respond to GET via XHR?

Topics: blog
Nov 25, 2010 at 11:34 AM


I've just begun integrating WebServer into my project.

I'm wondering if there is a simple example of returning data to XHR, or at least a documented means of achieving this, as this is my sole purpose for using an embeddable web server in my project.

Any help would be appreciated, documentation preferable :)


// Initialization
var server = new HttpServer.Server();
var module = new FileModule();
module.Resources.Add(new FileResources("/", Environment.CurrentDirectory + "\\wwwroot\\"));
var listener = HttpListener.Create(IPAddress.Any, 5050);
listener.RequestReceived += OnRequest;

// The OnRequest method
private void OnRequest(object sender, RequestEventArgs e)
	string echo = "ping";
	e.Response.Connection.Type = ConnectionType.Close;
	e.Response.Body.Write(Encoding.UTF8.GetBytes(echo), 0, echo.Length);

Feels like I'm close, but I am unsure of how to respond to the browser. Any help is appreciated!


Dec 29, 2010 at 4:07 PM

Ajax is just a regular request. There is no difference. Any sample project would do.

The thing with ajax requests is that you might not want to return an entire HTML page but only a part. Hence the need to be able to identify ajax requests.