求教:Failed to execute 'send' on 'XMLHttpRequest': failed to load 'http://www.baid

boylez 2014-12-07 09:11:05
报错
NetworkError: Failed to execute 'send' on 'XMLHttpRequest': failed to load 'http://www.baidu.com'.

换其他url也不行,实在看不出问题在哪,求教

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hottest News</title>
<body onload='GetData()'>
<script type="text/javascript">

var xmlhttp;
function GetData()
{
url = "http://www.baidu.com";
str = loadXMLDoc(url);
alert(str);

}



function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for all new browsers
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5 and IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,false);
try
{
xmlhttp.send();
return xmlhttp.responseText;
}
catch(e)
{
alert(e);
return null;
}
}
else
{
alert("Your browser does not support XMLHTTP.");
return null;
}
}

function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = OK
// ...our code here...
alert(2);
}
else
{
alert("a");
}
}
}
</script>
</body>

</html>
...全文
14515 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Blake0627 2019-01-01
  • 打赏
  • 举报
回复
改为异步请求
FuChenRenShen 2018-12-07
  • 打赏
  • 举报
回复
有可能是你没设置头部,xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");,不设置这个的话就不代表是个表单请求了
slwsss 2014-12-07
  • 打赏
  • 举报
回复
ajax不能跨域请求

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧