尝试从另一个域上的 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);
});
我尝试了多种方法格式化数据以使其正确解析,但我得到一个错误,即回调函数“未被调用”或存在“解析器错误”。下面是一个尝试,以人们在其他帖子中建议的格式返回一个字符串,但没有数据通过。
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>
我的ajax调用有什么问题?