I have a separate HTTP server hosted on another domain that I use as my backend server (C#). It creates a TcpListener, and on connection creates a new thread to deal with the connection.
I use jQuery to make an ajax get request to this server, and jQuery sends an OPTIONS request instead. After some research, I added this segment into my program in hopes that it would continue with the HTTP request afterwards. (ns is an instance of NetworkStream, from TcpClient.GetStream())
if (req1.StartsWith("OPTIONS"))
{
//req1 = req1.Split('\n')[0].Substring(9);
string headers = string.Format(
"HTTP/1.0 200 OK\r\n"+
"Access-Control-Allow-Origin: *\r\n"+
"Access-Control-Allow-Methods: *\r\n"+
"Access-Control-Max-Age: 1728000\r\n"+
"Access-Control-Allow-Headers: *\r\n"+
"Connection: Keep-Alive\r\n"+
"Content-Type: text/plain\r\n\r\n");
ns.Write(Encoding.ASCII.GetBytes(headers), 0, Encoding.ASCII.GetBytes(headers).Length);
ns.Close(); client.Close(); return;
}
Unfortunately, the browser keeps sending OPTIONS requests to the server, even though the server sends back Access-Control headers that should let it have unlimited access.
Here are the network logs for 1 of these requests: http://pastebin.com/T07Whvem
Any suggestions to make this work? The javascript code is pretty trivial, but I'll include it:
//this is just a long-poll
function waitForMsg(){
$.ajax({
url: "http://127.0.0.1:6969/2|||ub",
dataType: "text",
async: true,
cache: true,
timeout:50000,
success: function(data){
addmsg("new", data);
setTimeout(
'waitForMsg()',
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
'waitForMsg()',
"1000");
},
});
};
$(document).ready(function(){
waitForMsg(); /* Start the inital request */
});
I can't use an existing web-server because this is not really a standard webserver; merely a server that can take instructions via http.
Thanks!
You need to enable the below setting in jQuery
$(document).ready(function()
{
$.support.cors = true;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.