对 WCF Restful 服务的 Json 调用 (jquery) 始终运行错误函数

发布于 2024-12-18 16:12:43 字数 3760 浏览 1 评论 0原文

我正在尝试使用下面的 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”:“成功”}|“ParserError”| “未调用 jQuery1706816276672761887_1322704382563”

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

"[{\"学生ID\":256,\"学生名字\":\"阿里\",\"学生姓氏\":\"史密斯\"},{\"学生ID\":30 6、\"StudentFirstName\":\"John\",\"StudentSurname\":\"Menz\"},{\"StudentID\":314,\"StudentFirstName\" :\"George\",\"StudentSurname\":\"Ray\"},{\"StudentID\":316,\"StudentFirstName\":\"Fred\",\"StudentSu rname\":\"Patric\"},{\"StudentID\":603,\"StudentFirstName\":\"George\",\"StudentSurname\":\"Foster\"} ,{\"StudentID\":604,\"StudentFirstName\":\"Bobbie\",\"StudentSurname\":\"Kolman\"},{\"StudentID\":76 5、\"StudentFirstName\":\"Jim\",\"StudentSurname\":\"Khas\"},{\"StudentID\":987,\"StudentFirstName\":\ "Harry\",\"StudentSurname\":\"Poter\"},{\"StudentID\":988,\"StudentFirstName\":\"Con\",\"StudentSurn ame\":\"Mench\"},{\"StudentID\":1001,\"StudentFirstName\":\"Jim\",\"StudentSurname\":\"冒号\"}]"

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

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

myCallback([{"StudentID":256,"StudentFirstName":"Rachel","StudentSurname":"Smith"},{"StudentID":306,"StudentFirstName":"Ali","StudentSurname":"弗莱明"},{"StudentID":314,"学生ntFirstName":"乔治","StudentSurname":"雷"},{"StudentID":316,"StudentFirstName":"弗雷德","StudentSurname":"帕特里克"},{"StudentID":603,"StudentFirstName" :"乔治","学生姓氏":"F oster"},{"StudentID":604,"StudentFirstName":"Bobbie","StudentSurname":"Kolman"},{"StudentID":765,"StudentFirstName":"Jim","StudentSurname":"Khas" },{"学生ID":987,"学生姓名e":"Harry","StudentSurname":"Poter"},{"StudentID":988,"StudentFirstName":"Con","StudentSurname":"Mench"},{"StudentID":1001,"StudentFirstName" :"Jim","StudentSurname":"冒号"}]);

我还更改了我的 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

                    });

但我仍然遇到相同的错误。我为这样一个简单的任务花了三周的时间:(

I am trying to call my web service (WCF Restful) from a different domain (in the following code they are both in localhost but with different port number which I suppose considered as cross-domain request) using the JavaScript below.

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));
}

My web service is pretty simple. getStudents() returns a JSON string. I used JavaScriptSerializer to parse my student object to JSON.

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

    }  
}

I always get the following error from OnError function, and when I look at the response in Chrome development/debug mode I can see my JSON text.

Error:
success{"readyState":4,"status":200,"statusText":"success"}|"ParserError"|
"jQuery1706816276672761887_1322704382563 was not called"

My returned json text looks like this :

"[{\"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\":\"Colon\"}]"

Would you please help me to find a solution for this problem. I have already changed my web service from a SOAP to this restful one.

EDIT
I managed to get my jsonp message wraped in an method (myCallback as follow

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"}]);

And I also change my Ajax call as follow

$.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

                    });

But I still get the same error. I'm straggling for 3 weeks for such a simple task :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2024-12-25 16:12:43

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

Try changing getStudents() to return Json(List, JsonRequestBehavior.AllowGet) and accessing the returned list through a jQuery each using data[iterator].StudentID, data[iterator].StudentFirstName, etc.

云醉月微眠 2024-12-25 16:12:43

尝试以下更改:

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;
        ...
    }
}

Try this changes:

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;
    }
}

On the client-side:

function OnSuccess(data)
{
    for(var i=0;i<data.length;i++)
    {
        var fn = data[i].StudentFirstName;
        var sn = data[i].StudentSurName;
        ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文