跨域请求WCF数据服务使用json数据进行响应,但我在浏览器上收到javascript错误预期';'。此错误会阻止$ .ajax成功处理程序触发。无法弄清楚造成这种情况的原因。
提琴手
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Length: 368
Content-Type: application/json;charset=utf-8
Server: Microsoft-IIS/7.5
DataServiceVersion: 2.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 26 Mar 2011 00:19:20 GMT
{
"d" : {
"results": [
{
"__metadata": {
"uri": "http://subdomain.domain.com/services/Service1.svc/Surveys(410)", "type": "SurveyModel.Survey"
}, "OBJECTID": 410, "ID_Reviewer": "007", "ID_Reviewee": "007", "Rating": 9, "Question_A_Text": "test 1", "Question_B_Text": "test 2", "DateSubmitted": "\/Date(1301034211000)\/"
}
]
}
}
html的
<!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>
<title></title>
<script src="scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.ajax({
url: "http://subdomain.domain.com/services/Service1.svc/Surveys/?$format=json",
dataType: "jsonp",
success: function (data) {
alert("success");
}
});
});
</script>
</head>
<body>
</body>
</html>
答案 0 :(得分:0)
如果您正在使用跨域jsonp请求,您的响应应该包含在回调函数中,例如:http://search.twitter.com/search.json?callback=foo&q=terefere - 查看请求中的callback
参数,twitter服务查找这个参数并将响应包装到函数之后调用它。