【问题标题】:jquery wcf data service jsonp - javascript error Expected ';'jquery wcf数据服务jsonp - javascript错误预期';'
【发布时间】:2011-03-26 08:47:51
【问题描述】:

跨域请求 WCF 数据服务以 json 数据响应,但我在浏览器上收到 javascript 错误 Expected ';'。此错误会阻止 $.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>

【问题讨论】:

    标签: jquery wcf jsonp


    【解决方案1】:

    如果你使用跨域 jsonp 请求,你的响应应该被包装在一个回调函数中,像这里:http://search.twitter.com/search.json?callback=foo&q=terefere - 查看请求中的callback 参数,twitter 服务会寻找这个参数并包装响应在它的值之后调用的函数。

    【讨论】:

    猜你喜欢
    • 2014-08-20
    • 1970-01-01
    • 2016-04-10
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多