发送正确的XMLHttpRequest标头
Send the right XMLHttpRequest header
我正在尝试制作一个很棒的wifi芯片(esp8266)来与HTML网页通信。
因此,我使用了XMLHttpRequest。我知道我必须设置Access Control Allow Origin才能让它工作。。
我仍然在控制台中得到错误:
XMLHttpRequest无法加载http://x.x.x.x:8000/.请求的资源上不存在"Access Control Allow Origin"标头。因此,不允许访问源"null"
所以wifi模块正在发送这个标题:
WIFI模块响应
HTTP/1.1 200 OK'r'n
Access-Control-Allow-Origin: *'r'n
Content-Type: text/html'r'n
Hello world!'r'n
然后我试着用网页访问它:
JAVASCIPT
var xmlhttp;
function loadXMLDoc(){
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
console.log("readystate " + xmlhttp.readyState);
console.log("status " + xmlhttp.status);
console.log(xmlhttp.getAllResponseHeaders());
console.log(xmlhttp.responseText);
if (xmlhttp.readyState==4 && xmlhttp.status==200){
console.log(xmlhttp.responseText);
//document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://192.168.1.101:8000",true);
xmlhttp.send();
//console.log("status " + xmlhttp.status);
}
function send() {
xmlhttp.send("jooooo");
}
loadXMLDoc();
很抱歉,无法给出示例,因为它正在本地运行。
也许有人能给我一个调试方法?
更新
我可以在Chrome中观看标题。将显示在''r''n标头中。我可以发送200或404状态。但现在我必须弄清楚如何发送退货声明。在Google Chrome中查看HTTP标头?
因此,在我的镀铬控制台中,我得到了标题:"200 OK ''r''n''r''n原点:test''r''n访问控制允许原点:*''r''n''r''n内容类型:text/html/r''n''r''n Hello world!''r''n''r''n"
对于正常页面,我看到200 OK。所以''r''n不起作用。。。。我必须弄清楚如何发送退货声明。当我将200设置为404时,我会得到一个找不到的页面。所以收到了第一部分。。。
好的,我删除了''r''n字符。
我只是单独发送带有串行通信的标题。
我成功发送:HTTP://1.1 200 OK访问控制允许来源:*内容类型:text/html/
响应将加载到javascript中。。
- XMLHttpRequest未返回值-状态202
- XMLHttpRequest在移动设备上的chrome上不起作用
- AJAX简单错误.XMLHttpRequest无法加载http://localhost/mpl/getPage.php.
- 检查xmlhttprequest问题的消息
- 主线程上的同步XMLHttpRequest已弃用
- 如何定期发出xmlhttprequest
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 上传带有XMLHttprequest的文件-多部分/表单数据中缺少边界
- 对象#<XMLHttpRequest>没有方法'完成'
- 在XMLHttpRequest之后重新初始化jQuery
- 如何将GreaseMonkey中的XMLHttpRequest延迟到目标页面加载完成
- Javascript XMLHttpRequest——只有第一个POST请求有效
- XMLHttpRequest - difference between Chrome & Firefox
- IE上的新XMLHttpRequest()出现JS Ajax未指定错误
- 非常简单的XMLHttpRequest不起作用
- 如何使用XMLHttpRequest下载文件
- XMLHttpRequest在$.ajax工作的地方给出了CORS错误
- XMLHttpRequest.open()AJAX中的参数url
- XMLHTTPRequest脚本中没有internet连接和超时
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作