简体   繁体   English

使用C#后端服务器的跨域GET请求在jQuery中不起作用

[英]Cross-domain GET request not working in jQuery, with a C# backend server

I have a separate HTTP server hosted on another domain that I use as my backend server (C#). 我在另一个用作后端服务器(C#)的域上托管了一个单独的HTTP服务器。 It creates a TcpListener, and on connection creates a new thread to deal with the connection. 它创建一个TcpListener,并在连接时创建一个新线程来处理该连接。

I use jQuery to make an ajax get request to this server, and jQuery sends an OPTIONS request instead. 我使用jQuery向该服务器发出ajax获取请求,然后jQuery发送了OPTIONS请求。 After some research, I added this segment into my program in hopes that it would continue with the HTTP request afterwards. 经过一番研究,我将此段添加到程序中,希望以后可以继续使用HTTP请求。 (ns is an instance of NetworkStream, from TcpClient.GetStream()) (ns是NetworkStream的一个实例,来自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. 不幸的是,即使服务器发送回应该使其具有无限访问权限的Access-Control标头,浏览器仍会继续向服务器发送OPTIONS请求。

Here are the network logs for 1 of these requests: http://pastebin.com/T07Whvem 以下是这些请求之一的网络日志: http : //pastebin.com/T07Whvem

Any suggestions to make this work? 有什么建议可以使这项工作吗? The javascript code is pretty trivial, but I'll include it: javascript代码非常简单,但我将其包括在内:

//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; 我不能使用现有的Web服务器,因为它实际上不是标准的Web服务器。 merely a server that can take instructions via http. 仅仅是可以通过http接受指令的服务器。

Thanks! 谢谢!

You need to enable the below setting in jQuery 您需要在jQuery中启用以下设置

$(document).ready(function()
{
    $.support.cors = true;
});

see: http://api.jquery.com/jQuery.support/ 请参阅: http : //api.jquery.com/jQuery.support/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM