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

标签 jquery wcf json rest cross-domain

我正在尝试使用下面的 JavaScript 从不同的域调用我的 Web 服务(WCF Restful)(在下面的代码中,它们都位于本地主机中,但具有不同的端口号,我认为这是跨域请求)。

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 开发/ Debug模式下查看响应时,我可以看到我的 JSON 文本。

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

我返回的 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\":\"Colon\"}]"

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

编辑 我设法将 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

                    });

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

最佳答案

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

关于jquery - 对 WCF Restful 服务的 Json 调用 (jquery) 始终运行错误函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8335656/

相关文章:

javascript - 如何使用 Bootstrap 验证来验证所见即所得编辑器

WCF 服务 - 身份验证/SSPI 错误

c# - 序列化 TypeBuilder 列表

javascript - Vuejs 2 - 从 JSON 传递数据

json - 消费 org.glassfish.grizzly.utils.BufferInputStream@ Mule

java - RESTful 服务 : Unable to return array of object using json

jquery - 如何为导航栏添加事件类?

play 框架中的 Jquery 数据表 - $ ("#example").dataTable 不是函数

javascript - 当我从选择框中选择产品时,如何获取相关产品的价格?

c# - WCF 单一身份验证多端点