我想制作一个关于HTML/JS 同源政策的社区维基,希望能帮助任何人搜索这个主题.这是SO上搜索次数最多的主题之一,没有统一的wiki,所以我去:)
相同的源策略可防止从一个源加载的文档或脚本从另一个源获取或设置文档的属性.此政策可以追溯到Netscape Navigator 2.0.
请保持示例详细,最好还链接您的来源.
题:
我正在尝试使用JSON跨域,但我找到的只是JSON解析器,我不需要...
我已经读过可以使用JSON进行跨域请求,但到目前为止,我只看到了是使用XMLHttpRequest的实现...
- 这意味着你不能使用跨域请求,至少不是在IE 8之外...
我一直在http://www.json.org/,但我发现是解析器还是无用的.
到目前为止,我发现谷歌最好的是
http://devpro.it/JSON/files/JSONRequest-js.html,
但这相当混乱,不能跨域工作,域内也不行 - 或者更确切地说一点也不...
var the_object = {};
var http_request = new XMLHttpRequest();
http_request.open( "GET", url, true );
http_request.onreadystatechange = function () {
if ( http_request.readyState == 4 && http_request.status == 200 ) {
the_object = JSON.parse( http_request.responseText );
}
};
http_request.send(null);
Run Code Online (Sandbox Code Playgroud) 为什么以下代码基于Mozilla示例不起作用?尝试使用Firefox 3.5.7和Chrome.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
</body>
<script>
var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', false);
req.send();
if(req.status == 200) {
alert(req.responseText);
}
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
请浏览器从本地磁盘中提取html(文件:/// C:/Users/Maxim%20Veksler/Desktop/XMLHTTP.html)
在Firefox上它会出现以下错误:
uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: file:///C:/Users/Maxim%20Veksler/Desktop/XMLHTTP.html :: <TOP_LEVEL> :: line 10" data: no]
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我想向远程主机提交请求并提醒结果(稍后添加到div中).
我试图通过ajax(通过XmlHttpRequest(= xhr)在Javascript中"流式传输"(从服务器到客户端).我使用的是在"HTTP Streaming"(推送)AJAX模式的跨浏览器实现中描述的修改后的handleResponse函数
function handleResponse() {
if (http.readyState != 4 && http.readyState != 3)
return;
if (http.readyState == 3 && http.status != 200)
return;
if (http.readyState == 4 && http.status != 200) {
clearInterval(pollTimer);
inProgress = false;
}
// In konqueror http.responseText is sometimes null here...
if (http.responseText === null)
return;
while (prevDataLength != http.responseText.length) {
if (http.readyState == 4 && prevDataLength == http.responseText.length)
break;
prevDataLength = http.responseText.length;
var response = http.responseText.substring(nextLine);
var lines = response.split('\n');
nextLine = …Run Code Online (Sandbox Code Playgroud)