I’m trying to write a simple http server as a part of an application. It needs to respond to Http requests submitted with ajax on 127.0.0.1:someport with a simple text response. A javascript app will poll (or eventually longpoll) for information on the local users actions.
At the moment I’ve got an httpListener that calls the following as part of its callback processing:
private void ProcessRequest(HttpListenerContext context)
{
var stream = context.Response.OutputStream;
var resp = Encoding.UTF8.GetBytes("This is a test response" + DateTime.Now.ToShortTimeString() + "\r\n\r\n");
stream.Write(resp, 0, resp.Length);
stream.Close();
}
Which provides a result if called from the browser, but an error if called with $.ajax.
What is the minimum I need to return to get a sucess response to a query such as
$.ajax({
url: 'http://127.0.0.1:12345/',
dataType: 'text',
success: function (data) {
$('#res').html(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert(textStatus);
}
}
);
Edit:
I believe I have avoided the cross site scripting issues by running
netsh http add urlacl url=http://*:12345/app user=domain\user
and can trace that the ProcessRequest is called
I suspect that your web service works fine. If you’re calling it from a file URL, however, your document will not be able to call any URLs hosted under localhost:12345 because it’s considered cross-domain. Try hosting the HTML and JavaScript under the same web site; you may see more success doing so.