我在使用jQuery AJAX从“支持AJAX的WCF服务”中的WebGet函数检索数据时遇到了问题。该服务代码如下所示:wcf/ajax模糊错误“error”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace SPA
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class db
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[WebGet]
[OperationContract]
public IEnumerable<Geofence> GetGeofences()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/json";
var dc = new AtomnetDataContext();
return dc.Geofences;
}
// Add more operations here and mark them with [OperationContract]
}
}
这是试图调用它的代码:放置一个断点
$(function() {
$.get("db.svc/GetGeofences", alert);
});
内的服务方法的代码表明,它确实调用。我证实,通过将dc.Geofences.ToArray()实现成一个变量(未在示例中显示)成功获取数据。 Geofence
是Linq2sql生成的类型。
将调用转换为明确的ajax调用$.ajax({ ... });
将错误对象返回给错误函数,但其中包含的消息仅仅表示“错误”,这不如说明性的。
使用相当于Firebug的IE10检查网络流量显示该呼叫是“(中止)”。此问题已将设置为服务配置,因为该调用尽可能返回值。
看起来有一个序列化异常,然后是一个通信异常,这可能是相应的。
A first chance exception of type 'System.Runtime.Serialization.SerializationException'
occurred in System.Runtime.Serialization.dll A first chance exception of type
'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.Web.dll