我目前正在开发一个PC网络服务器,将访问我的MCU网络服务器使用此代码。
PC机和MCU连接在同一网络中。每当我尝试通过jquery向我的MCU服务器发送GET请求时,我都得不到响应。虽然我看到MCU收到了请求,但我的PC see服务器没有收到响应。
会不会是因为我有跨域访问的问题?但是为什么呢?它们只是连接到同一个网络,对吧?
<!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 the服务器进行跨域访问,或者只是我的PC the服务器需要它才能访问MCU the服务器?
这是我监控PC MCU服务器和MCU MCU服务器的结果。MCU MCU服务器响应似乎缺少其他标头,因为它仅在Content-length: 1121处停止

发布于 2014-02-20 19:06:15
为此,您还应该在客户端启用内核。例如,
$.support.cors = true;
$.ajax({
crossDomain: true,
type: "POST",
url: "url"
}).done(function (data) {
alert(data);
});https://stackoverflow.com/questions/21905752
复制相似问题