Javascript HttpRequest从JS到C#Http侦听器
我有这个代码从JS发送一个httprequestJavascript HttpRequest从JS到C#Http侦听器,javascript,c#,httprequest,httplistener,Javascript,C#,Httprequest,Httplistener,我有这个代码从JS发送一个httprequest var xhr = new XMLHttpRequest(); xhr.open("GET", "http://localhost:60006/", true); xhr.onload = function () { alert('onload'); }; xhr.onreadystatechange = function () { alert('statechange')
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:60006/", true);
xhr.onload = function () {
alert('onload');
};
xhr.onreadystatechange = function () {
alert('statechange');
};
xhr.send();
此代码正确发送http请求,
作为HttpListener,我在c#上有这段代码
static void Main()
{
var web = new HttpListener();
web.Prefixes.Add("http://localhost:60006/");
web.Start();
var context = web.GetContext();
var response = context.Response;
const string responseString = "<html><body>Hello world</body></html>";
var buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
var output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
web.Stop();
Console.ReadKey();
}
static void Main()
{
var web=newhttplistener();
web.Prefixes.Add(“http://localhost:60006/");
web.Start();
var context=web.GetContext();
var-response=context.response;
常量字符串responseString=“Hello world”;
var buffer=System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64=buffer.Length;
var输出=response.OutputStream;
输出.写入(缓冲区,0,缓冲区.长度);
output.Close();
web.Stop();
Console.ReadKey();
}
C#代码接收到来自JS的httpRequest,但我无法获得JS上的响应值(更改statechange和onload上的警报)
任何帮助都将不胜感激您在控制台中看到了什么?了解同源策略。您在控制台上看到了什么错误?在控制台中看到了什么?了解同源策略。您在控制台上遇到的错误是什么?