连接到REST服务时,AJAX响应为空
我创建了一个WCF REST服务,它只返回一个类似于{“Id”:10,“Name”:“Peter”}的JSON,使用Chrome我可以看到这个响应,但是使用下面的HTML5,状态为0。谁能告诉我我做错了什么?谢谢连接到REST服务时,AJAX响应为空,ajax,html,rest,Ajax,Html,Rest,我创建了一个WCF REST服务,它只返回一个类似于{“Id”:10,“Name”:“Peter”}的JSON,使用Chrome我可以看到这个响应,但是使用下面的HTML5,状态为0。谁能告诉我我做错了什么?谢谢 <!DOCTYPE html> <html> <head> <title>Company Name</title> <link rel="shortcut icon" href="
<!DOCTYPE html>
<html>
<head>
<title>Company Name</title>
<link rel="shortcut icon" href="favicon.ico"/>
<script>
window.onload = function()
{
var url = "http://localhost:8732/DataService/data/10";
request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if(request.readyState == 4)
{
if(request.status == 404)
{
document.getElementById("content").innerHTML = "No page found";
}
if(request.status == 2 || request.status == 200)
{
document.getElementById("content").innerHTML = "<br /><br /><br />Response: " + request.responseText;
}
else
{
document.getElementById("content").innerHTML = "Status: " + request.status +" ReadyState: " + request.readyState + "<br /> " + request.responseText;
}
}
}
request.open("GET", url, true);
request.send(null);
}
</script>
</head>
<body>
<div id="container" style="width:100%;">
<div id="header" style="position:absolute; height:100px;width:inherit;">
<img src="Dialog.bmp" alt="Missing" />
</div>
<!-- <div id="menu" style="background-color:#33CCFF; height:200px;width:100px;float:left;">
<b>Menu</b><br>
HTML<br>
CSS<br>
JavaScript</div> margin:0 auto; height:100%;width:100%;-->
<br /><br /><br /><br />
<div id="content" style="background-color:#333333; height:500px;width:inherit;float:left; color:white">Content goes here</div>
<div id="footer" style="background-color:#00A8FF;clear:both;text-align:center;color:white;font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif">Copyright © CompanyName</div>
</div>
</body>
</html>
公司名称
window.onload=函数()
{
变量url=”http://localhost:8732/DataService/data/10";
请求=新的XMLHttpRequest();
request.onreadystatechange=函数()
{
if(request.readyState==4)
{
if(request.status==404)
{
document.getElementById(“内容”).innerHTML=“未找到页面”;
}
if(request.status==2 | | request.status==200)
{
document.getElementById(“内容”).innerHTML=“
Response:“+request.responseText;
}
其他的
{
document.getElementById(“内容”).innerHTML=“状态:”+request.Status+“ReadyState:“+request.ReadyState+””
“+request.responseText;
}
}
}
打开(“获取”,url,true);
请求发送(空);
}
内容在这里
版权所有©公司名称
看起来我在做跨源请求,IE+9允许,但Chrome不允许
现在,我已将JSON WCF请求移动到Apache转发的PHP cURL。使用IE打开此HTML5时,正确接收到JSON响应。