Json调用(jquery)到一个WCF restful服务总是运行错误函数



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

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

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

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

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

错误:"判别"成功{:4,"状态":200年,"statusText":"成功"}| |"ParserError"jQuery1706816276672761887_1322704382563未被调用"

返回的json文本是这样的:

"[{"StudentID ":256年,"StudentFirstName ","阿里","StudentSurname ","史密斯"},{"StudentID ":306年,"StudentFirstName ","约翰","StudentSurname ","Menz "},{"StudentID ":314年,"StudentFirstName ","乔治","StudentSurname ","雷"},{"StudentID ":316年,"StudentFirstName ","弗雷德","StudentSurname ","帕特里克"},{"StudentID ":603年,"StudentFirstName ","乔治","StudentSurname ","福斯特"},{"StudentID ":604年,"StudentFirstName ","博比","StudentSurname ","身"},{"StudentID ":765年,"StudentFirstName ":"吉姆","StudentSurname ":"哈斯"},{"StudentID ":987年,"StudentFirstName ","哈利","StudentSurname ","产业价值链"},{"StudentID ":988年,"StudentFirstName ","Con ","StudentSurname ","Mench "},{"StudentID ":1001年,"StudentFirstName ","吉姆","StudentSurname ","结肠"}]"

你能帮我找到一个解决这个问题的方法吗?我已经将我的web服务从SOAP更改为这种restful的web服务。

编辑我设法得到我的json消息包装在一个方法(myCallback如下

myCallback([{"StudentID":256年,"StudentFirstName":"瑞秋"、"StudentSurname":"史密斯"},{"StudentID":306年,"StudentFirstName":"阿里"、"StudentSurname":"弗莱明"},{"StudentID":314年,"StudentFirstName":"乔治"、"StudentSurname":"雷"},{"StudentID":316年,"StudentFirstName":"弗雷德"、"StudentSurname":"帕特里克"},{"StudentID":603年,"StudentFirstName":"乔治"、"StudentSurname":"培养"},{"StudentID":604年,"StudentFirstName":"博比"、"StudentSurname":"身"},{"StudentID":765年,"StudentFirstName":"吉姆","StudentSurname":"哈斯"},{"StudentID":987年,"StudentFirstName":"哈利","StudentSurname":"价值链"},{"StudentID":988年,"StudentFirstName":"缺点","StudentSurname":"Mench"},{"StudentID":1001年,"StudentFirstName":"吉姆","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
                    });

但是我仍然得到相同的错误。为了这么简单的任务,我已经挣扎了3个星期了:(

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

试试这个改变:

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

相关内容

  • 没有找到相关文章

最新更新