【问题标题】:Does the client and webserver need to have cross domain access enabled?客户端和网络服务器是否需要启用跨域访问?
【发布时间】:2014-03-21 06:35:43
【问题描述】:

我目前正在开发一个 PC 网络服务器,它将使用此代码访问我的 MCU 网络服务器。

PC 和 MCU 连接在同一个网络中。 每当我尝试通过 jquery 向我的 MCU 服务器发送 GET REQUEST 时,我都没有收到任何响应。虽然我看到 MCU 收到了请求,但我的 PC 网络服务器中没有收到响应。

会不会是我的跨域访问有问题?但为什么?他们只是连接到同一个网络,对吧?

<!DOCTYPE html>
<html>
<?php
        header("Access-Control-Allow-Origin: *");
?>
<head>

    <script src="jquery-2.0.3.min.js"></script>
    <script src="jquery-1.9.1.js"></script>
    <script src="jquery-ui.js"></script>
    <script>$(document).ready(function()
        { 
            $("button").click(function()
            {    
                $.get("192.168.1.102/",function(data,status)
                {     
                    alert("Data: " + data + "\nStatus: " + status);   
                });  
            });
        });
    </script>
</head>
<body>

    <button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>

我的 MCU 网络服务器没有文件 index.html 或 .php,但它会在收到 GET 或 POST 请求时发送一​​个字符串。

这是它发送的字符串:

#define HTTP_HEAD "HTTP/1.1 200 OK\r\n" \
    "Content-Type: text/html\r\n" \
    "Connection: Close\r\n" \
    "Content-Length: 1121\r\n\r\n" \
    "<!DOCTYPE html>" \
    "<html>" \
        "<head>" \
            "<style>" \
                    "#content{color: red; font-size: 50px;top: 70px;}" \
            "</style>" \
        "</head>" \
            "<body>" \
            "<center>" \
            "<div id='content'>" 

#define HTTP_TAIL "</center></body></html>\r\n\r\n"

我是否还需要我的 MCU webserve 来进行跨域访问,还是只是我的 PC 网络服务器需要它来访问 MCU 网络服务器?

这是我同时监控 PC 网络服务器和 MCU 网络服务器的结果。 MCU 网络服务器响应似乎缺少其他标头,因为它只停在 Content-length: 1121

【问题讨论】:

    标签: php jquery html ajax cross-domain


    【解决方案1】:

    为此,您也应该在客户端启用内核。 例如,

        $.support.cors = true;
         $.ajax({
            crossDomain: true,
            type: "POST",
            url: "url"
    
        }).done(function (data) {
            alert(data);
        });
    

    【讨论】:

    • 谢谢阿努普!我会尝试这样做,但现在我想与您分享我在监控来自 GET 请求的 PC 网络服务器和接收 GET 请求的 MCU 网络服务器时发现的结果。 [这里是链接] [i149.photobucket.com/albums/s48/vash1102/guide/…我只是想知道为什么MCU给出的响应停止在Content-length:1121 hmmm.....
    猜你喜欢
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 2012-04-23
    • 2011-02-10
    相关资源
    最近更新 更多