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
jQuery跨域调用REST服务_Jquery_Wcf_Webservice Client - Fatal编程技术网

jQuery跨域调用REST服务

jQuery跨域调用REST服务,jquery,wcf,webservice-client,Jquery,Wcf,Webservice Client,正在尝试从另一个域上的REST WCF服务获取数据。下面是我的服务的创建,它看起来很好,接收带有我的参数和jQuery时间戳函数签名的请求 _Service = new WebServiceHost(myObject); var binding = new WebHttpBinding() { CrossDomainScriptAccessEnabled = true }; var endpoint = _Service.AddServiceEndpoint(typeof(myObjectCon

正在尝试从另一个域上的REST WCF服务获取数据。下面是我的服务的创建,它看起来很好,接收带有我的参数和jQuery时间戳函数签名的请求

_Service = new WebServiceHost(myObject);
var binding = new WebHttpBinding() { CrossDomainScriptAccessEnabled = true };
var endpoint = _Service.AddServiceEndpoint(typeof(myObjectContract), binding, "http://localhost:8000/myObject_Service");
            endpoint.Behaviors.Add(new WebHttpBehavior());
            _Service.Open();
这是jQuery调用

$.ajax({
    url: "http://localhost:8000/myObject_Service/GetInterfaceInfo?interfaceId=1&callback=?",
    crossDomain: true,
    dataType: "jsonp",
    success: function (data) {
        alert(JSON.stringify(data));
    }
}).fail(function (data, textStatus, errorThrown) {
    alert(textStatus);
});
我尝试了多种方法格式化数据,以使其正确解析,但我要么得到一个错误,即回调函数“未调用”,要么存在“parsererror”。下面是一个尝试,以人们在其他帖子上建议的格式返回一个字符串,但没有数据通过

jQuery17106912882712204009_1384296559027({"data":"1","data":"2"})
这是从服务发送的数据

public string GetInterfaceInfo(int interfaceId, string callback)
{
    return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(_I);
    //Tried this below, also with no success.
    //return string.Format("{0}({1})", callback, output);
}
我在ajax调用中没有收到任何数据,尽管它确实可以工作,因为在浏览器调用中。我得到了所有这些数据

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
[{"Id":100,"Status":true,"Direction":"Inbound","Name":"Test_100","Sent":0,"Received":1000,"LastMessage":"2013-11-13 08:50:33.AM","Filtered":0,"CanRestart":true},{"Id":101,"Status":false,"Direction":"Inbound","Name":"Test_101","Sent":0,"Received":1000,"LastMessage":"2013-11-13 08:50:33.AM","Filtered":0,"CanRestart":true},{"Id":102,"Status":true,"Direction":"Inbound","Name":"Test_102","Sent":0,"Received":1000,"LastMessage":"2013-11-13 08:50:33.AM","Filtered":0,"CanRestart":true}]
</string>

[{“Id”:100,“Status”:true,“Direction”:“Inbound”,“Name”:“Test_100”,“Sent”:0,“Received”:1000,“LastMessage”:“2013-11-13 08:50:33.AM”,“Filtered”:0,“CanRestart”:true},{“Id”:101,“Direction”:“Inbound”,“Name”:“Test_101”,“Sent”:0,“Received”:1000,“LastMessage”:“2013-11-13 08:50:33.AM”,“Filtered”:0,“CanRestart”:true},{“Id”:102,“Status”:true,“方向”:“入站”,“名称”:“测试单元102”,“发送”:0,“接收”:1000,“最后消息”:“2013-11-13 08:50:33.AM”,“过滤”:0,“可重新启动”:true}]

我的ajax调用有什么问题?

我的服务合同中的属性似乎有问题,应该是
ResponseFormat=WebMessageFormat.Json,尽管我认为Json应该是默认类型。

对于最后三个问题,不,不,不。您是手动生成
jquery1716912882712204009\u 1384296559027
,还是jQuery告诉您的服务器返回的内容。到目前为止,除了
之外,一切看起来都很好>crossDomain:true
,它不是必需的,但不应该使您的请求失败。当您遇到解析错误时,是否打开javascript控制台查看解析失败的responsetext?它是否返回到成功或失败功能?如果通过浏览器通过网络检查检查请求,则响应和响应是什么de?它正在返回失败功能。我无法检查响应文本。我使用Chrome开发工具调试脚本。我是否应该使用其他工具来提供有关失败的更多详细信息?单击网络选项卡,它将为您提供有关请求的所有信息。(别忘了单击请求)