0

i'm doing my baby steps in web-development.

I have a Html+JS(jQuery) Frontend and a C# Backend.

For now i just want a ping-pong request/response.

The JS looks like this :

$(document).ready(function() {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open( "GET", "http://testhttp/", false ); // false for synchronous request
  xmlHttp.send();
  console.log(xmlHttp.responseText);
});

The C# looks like this

if (!HttpListener.IsSupported)
{
    ...
}

string[] prefixes = {"http://testhttp/"};

// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
    listener.Prefixes.Add(s);
}
listener.Start();

Console.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request. 
HttpListenerContext context = listener.GetContext();

HttpListenerRequest request = context.Request;

// Obtain a response object.
HttpListenerResponse response = context.Response;       

// Construct a response.
string responseString = "<p> Hello world!</p>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer,0,buffer.Length);
// You must close the output stream.
output.Close();
listener.Stop();

The Backend receives the request. However the response will not be transmitted correctly or permission is denied (on Firefox and Chrome).

jquery-3.2.0.min.js:2 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://testhttp/'.

I read that it might has something to do with the origin of the response and i need to set Access-Control-Allow-Origin. But those attempts failed. Can someone pls guide me here?

Edit Based on comments the js looks like this

$(document).ready(function() {
   $.ajax({
        url: "http://testhttp/",
        type: "GET",
        crossDomain: true,
        dataType: "json",
        success: function (response) {
            $('#Test').html(response);
        },
        error: function (xhr, status) {
            alert("error");
        }
    });
});

and in C# backend i added

response.Headers["Access-Control-Allow-Origin"] = "*";

Getting

GET http://testhttp/ net::ERR_CONNECTION_RESET

Terry Storm
  • 497
  • 3
  • 14
  • Firstly, most browser no longer support synchronous ajax, secondly, jQuery has `$.ajax` which makes things a whole lot easier? – adeneo Apr 06 '17 at 13:52
  • are you on https when making the request? –  Apr 06 '17 at 13:52
  • http://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin-resource-sharing-cors-post-request-working – epascarello Apr 06 '17 at 13:54
  • One wonders why the error seems to be generated by jQuery, when the code uses `XMLHttpRequest` ? – adeneo Apr 06 '17 at 13:57
  • @Orangesandlemons everything is local for now (redirect request to localhost) – Terry Storm Apr 06 '17 at 14:24
  • @adeneo i read its deprecated, but thought its still working though. I edit my js to use ajax. sadly still doesnt work. any ideas? – Terry Storm Apr 06 '17 at 14:26
  • @TerryStorm you may need to add access-control-allow-methods. (search SO for your error message for more info) –  Apr 06 '17 at 14:39

0 Answers0