【问题标题】:Json call (jquery) to a WCF restful service always run error function对 WCF restful 服务的 Json 调用(jquery)总是运行错误函数
【发布时间】:2012-01-10 06:17:33
【问题描述】:

我正在尝试使用下面的 JavaScript 从不同的域调用我的 Web 服务 (WCF Restful)(在以下代码中,它们都在 localhost 但具有不同的端口号,我认为这被视为跨域请求)。

jQuery.support.cors = true;
$(document).ready(function () {
    {
        $.ajax({
            type: "POST",
            url: "http://localhost:52032/Service1.svc/std",
            contentType: "application/jsonp; charset=utf-8",
            dataType: "jsonp",

            processData: false ,    //Same result if i remove this line
            crossDomain : true  , //Same result if i remove this line
            converters: window.String, //Same result if i remove this line
            success: OnSuccess,
            error: OnError});
    }
});

function OnSuccess(data)
{
    alert("Finaly :D");
    JSON.stringify(data);
}

function OnError(request, status, error)
{
    alert("Error: "+request.statusText + JSON.stringify(request)+  " | " +JSON.stringify(status) +" | " +JSON.stringify(error));
}

我的网络服务非常简单。 getStudents() 返回一个 JSON 字符串。我使用JavaScriptSerializer 将我的学生对象解析为 JSON。

namespace MyFirstWcfTestService
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract(Name = "AddParameter")]
        [WebGet(UriTemplate = "/std", ResponseFormat = WebMessageFormat.Json)]
        String getStudents();

    }  
}

我总是从OnError 函数中收到以下错误,当我在 Chrome 开发/调试模式下查看响应时,我可以看到我的 JSON 文本。

错误: 成功{"readyState":4,"status":200,"statusText":"success"}|"ParserError"| "没有调用 jQuery1706816276672761887_1322704382563"

我返回的 json 文本如下所示:

"[{\"StudentID\":256,\"StudentFirstName\":\"Ali\",\"StudentSurname\":\"Smith\"},{\"StudentID\":306,\" StudentFirstName\":\"John\",\"StudentSurname\":\"Menz\"},{\"StudentID\":314,\"StudentFirstName\":\"George\",\"StudentSurname\": \"Ray\"},{\"StudentID\":316,\"StudentFirstName\":\"Fred\",\"StudentSurname\":\"Patric\"},{\"StudentID\":603, \"StudentFirstName\":\"George\",\"StudentSurname\":\"Foster\"},{\"StudentID\":604,\"StudentFirstName\":\"Bobbie\",\"StudentSurname\ ":\"Kolman\"},{\"StudentID\":765,\"StudentFirstName\":\"Jim\",\"StudentSurname\":\"Khas\"},{\"StudentID\": 987,\"StudentFirstName\":\"Harry\",\"StudentSurname\":\"Poter\"},{\"StudentID\":988,\"StudentFirstName\":\"Con\",\" StudentSurname\":\"Mench\"},{\"StudentID\":1001,\"StudentFirstName\":\"Jim\",\"StudentSurname\":\"冒号\"}]"

您能帮我找到解决这个问题的方法吗?我已经将我的 Web 服务从 SOAP 更改为这个宁静的服务。

编辑 我设法将我的 jsonp 消息包装在一个方法中(myCallback 如下

myCallback([{"StudentID":256,"StudentFirstName":"Rachel","StudentSurname":"Smith"},{"StudentID":306,"StudentFirstName":"Ali","StudentSurname":" Flemming"},{"StudentID":314,"StudentFirstName":"George","StudentSurname":"Ray"},{"StudentID":316,"StudentFirstName":"Fred","StudentSurname":"Patric" },{"StudentID":603,"StudentFirstName":"George","StudentSurname":"Foster"},{"StudentID":604,"StudentFirstName":"Bobbie","StudentSurname":"Kolman"}, {"StudentID":765,"StudentFirstName":"Jim","StudentSurname":"Khas"},{"StudentID":987,"StudentFirstName":"Harry","StudentSurname":"Poter"},{" StudentID":988,"StudentFirstName":"Con","StudentSurname":"Mench"},{"StudentID":1001,"StudentFirstName":"Jim","StudentSurname":"colon"}]);

我也改变了我的 Ajax 调用如下

$.ajax({
                        type: "post",
                        url: "http://localhost:52032/Service1.svc/getStd?callback=myCallback",  //or callback=?
                        dataType: "jsonp",
                        //converters: jQuery.parseJSON,
                        contentType: 'application/javascript',
                        success: OnSuccess,
                        error: OnError

                    });

但我仍然遇到同样的错误。我为这样一个简单的任务奋斗了 3 周:(

【问题讨论】:

  • 如果 getString() 返回一个 json 格式的字符串,我认为没有必要指定 ResponsFormat
  • 还尝试检查配置中的 automaticFormatSelectionEnabled 是否为 false:
  • 当我删除响应格式时,我得到的响应是一个带有字符串标签的 Xml 响应,其中包括我的 json 文本

标签: jquery wcf json rest cross-domain


【解决方案1】:

尝试将 getStudents() 更改为返回 Json(List, JsonRequestBehavior.AllowGet) 并通过 jQuery 访问返回的列表,每个使用 data[iterator].StudentID、data[iterator].StudentFirstName 等。

【讨论】:

    【解决方案2】:

    试试这个改变:

    namespace MyFirstWcfTestService
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract(Name = "AddParameter")]
            [WebGet(UriTemplate = "/std", ResponseFormat = WebMessageFormat.Json)]
            List<StudentDTO>getStudents();
    
        }  
    
        class StudentDTO
        {
           public int StudentID;
           public string StudentFirstName;
           public string StudentSurname;
        }
    }
    

    在客户端:

    function OnSuccess(data)
    {
        for(var i=0;i<data.length;i++)
        {
            var fn = data[i].StudentFirstName;
            var sn = data[i].StudentSurName;
            ...
        }
    }
    

    【讨论】:

    • Thx Jerjer 我仍然收到问题中提到的解析器错误。你知道为什么它在成功收到响应时调用 OnError 函数吗? "错误:成功{"readyState":4,"status":200,"statusText":"success"}|"ParserError"| "jQuery1706816276672761887_1322704382563 未被调用""
    • 我很困惑,可以尝试另一个 jquery 版本吗?
    • 我已经按照你的建议尝试了不同的 jQuery 版本,但我得到了同样的错误。我认为我的 json 响应不合适。如果 jquery.Ajax 需要,我有什么方法可以生成 jsonP 而不是 json?(json 响应在我的初始问题正文中)
    猜你喜欢
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多