Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对WCF restful服务的Json调用(jquery)始终运行错误函数_Jquery_Wcf_Json_Rest_Cross Domain - Fatal编程技术网

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

对WCF restful服务的Json调用(jquery)始终运行错误函数,jquery,wcf,json,rest,cross-domain,Jquery,Wcf,Json,Rest,Cross Domain,我试图在下面的代码中从不同的域调用我的web服务WCF Restful,它们都在localhost中,但端口号不同,我想使用下面的JavaScript将其视为跨域请求 jQuery.support.cors = true; $(document).ready(function () { { $.ajax({ type: "POST", url: "http://localhost:52032/Service1.svc/std

我试图在下面的代码中从不同的域调用我的web服务WCF Restful,它们都在localhost中,但端口号不同,我想使用下面的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文本

错误: 成功{readyState:4,status:200,statusText:success}解析错误| 未调用JQuery17068162766727618871322704382563

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

[{\StudentID\:256、\StudentFirstName\:\Ali\,\studentname\:\Smith\,{\StudentID\:306、\StudentFirstName\:\John\,\studentname\:\Menz\,{\StudentID\:314、\StudentFirstName\:\George\,\studentname\:\Ray\,{\StudentID\:316、\StudentFirstName\:\Fred\,\studentname\:\Patric,{\StudentID\:604、\StudentFirstName\:\Bobbie\,\StudentName\:\Kolman\,{\StudentID\:765、\StudentFirstName\:\Jim\,\StudentName\:\Khas\,{\StudentID\:987、\StudentName\:\Poter\,{\StudentID\:988、\StudentFirstName\:\Con\,\StudentName\:\Mench\,{\StudentID\:1001、\StudentName\:\Jim\,\StudentColon\]

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

编辑 我设法将我的jsonp消息包装在方法myCallback中,如下所示

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

                    });
myCallback[{StudentID:256,StudentFirstName:Rachel,StudentName:Smith},{StudentID:306,StudentFirstName:Ali,StudentName:Flemming},{StudentID:314,StudentFirstName:George,StudentName:Ray},{StudentID:316,StudentFirstName:Fred,StudentName:Patric},{StudentID:603,StudentFirstName:George,StudentName:Foster},{StudentID:604,StudentFirstName:Bobbie,StudentName:Kolman},{StudentID:765,StudentFirstName:Jim,StudentName:Khas},{StudentID:987,StudentFirstName:Harry,StudentName:Poter},{StudentID:988,StudentFirstName:Con,StudentName:Mench},{StudentID:1001,StudentFirstName:Jim,StudentName: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周的时间来完成这样一个简单的任务:

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

尝试更改getStudents以返回JsonList、JsonRequestBehavior.AllowGet,并通过jQuery访问返回的列表,每个查询使用数据[iterator]。StudentID、数据[iterator]。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;
        ...
    }
}
尝试以下更改:

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

如果getString返回一个json格式的字符串,我认为没有必要指定ResponseFormat也尝试检查AutomaticFormatSelectEnabled from config是否为false:当我删除响应格式时,我得到的响应是一个Xml响应,带有一个字符串标记,包括我的json文本如果getString返回一个json格式的字符串,我认为没有need指定ResponseFormat还尝试检查AutomaticFormatSelectEnabled from config是否为false:当我删除响应格式时,我得到的响应是带有字符串标记的Xml响应,其中包括我的json textThx Jerjer,我仍然得到我在问题中提到的parsererror。你知道为什么它在重新启动时调用OneError函数吗成功接收响应??错误:成功{readyState:4,状态:200,状态文本:成功}|ParserError | jQuery17068162766727618871322704382563没有被称为im困惑,可以尝试另一个jquery版本吗?我已经按照您的建议尝试了不同的jquery版本,但我得到了相同的错误。我假设我的json响应不合适。如果jquery.Ajax需要json,我是否有任何方法可以生成jsonP而不是json?json响应是正确的我最初的问题bodyThx Jerjer我仍然得到我在问题中提到的parsererror。你知道为什么它在成功接收响应时调用OneError函数吗?错误:success{readyState:4,status:200,statusText:success}|ParserError | jQuery17068162766727618871322704382563没有被称为im困惑,可以尝试另一个jquery版本吗?我已经按照您的建议尝试了不同的jquery版本,但我得到了相同的错误。我假设我的json响应不合适。如果jquery.Ajax需要json,我是否有任何方法可以生成jsonP而不是json?json响应是正确的我最初的问题是