ASMX webservice不返回JSON,只能使用application / x-www-form-urlencoded contentType进行POST

我可以使用jQuery IF调用我的web服务contentType =“application / x-www-form-urlencoded; charset = utf-8”

但是,这将返回xml: [myjson]

如果我尝试使用“application / json; charset = utf-8”对服务进行POST,我会收到500错误,其中包含空StackTrace和ExceptionType。 我的网络服务function从未被打过,所以我不太确定如何调试这种情况。

我的方法和类使用适当的属性进行修饰,并设置为使用JSON作为其响应类型(与我的wsdl和disco文件一样)。 我安装了Ajax扩展和web.config中的必需条目。

这是在SharePoint服务器场上,但我不确定这会产生太大的影响。 我在所有WFE上部署了web.config更改,并安装了ajax扩展。 服务再次起作用,除了默认内容类型之外,它不会接受任何其他内容。

不知道我在这里缺少什么,伙计……

我的ajax电话:

 $.ajax({ type: "POST", url: "/_vti_bin/calendar.asmx/Test", dataType: "json", data: "{}", contentType: "application/json; charset=UTF-8", success: function(msg){ alert(msg); }, error: function(xhr, msg){ alert(msg + '\n' + xhr.responseText); } }); 

我的webservice类:

 [WebService(Namespace = "http://namespace")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService()] public class CalendarService : WebService { [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string Test() { return "Hello World"; } } 

我使用Web服务在2.0中工作,但是我已经从.d中获得了保护(参见下面的dataFilter)。 我也正在返回一个对象数组。 注意:对象的类是静态的,或者它至少对我不起作用。

  $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", data: "{}", dataFilter: function(data) { var msg; if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') msg = JSON.parse(data); else msg = eval('(' + data + ')'); if (msg.hasOwnProperty('d')) return msg.d; else return msg; }, url: "webservice/ModifierCodesService.asmx/GetModifierList", success: function(msg) { LoadModifiers(msg); }, failure: function(msg) { $("#Result").text("Modifiers did not load"); } }); 

这是我的网络服务的片段:

 [WebService(Namespace = "http://mynamespace.com/ModifierCodesService/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [ScriptService] public class ModifierCodesService : System.Web.Services.WebService { ///  /// Get a list of Modifiers ///  ///  [WebMethod(EnableSession = true)] public Modifier[] GetModifierList() { return GetModifiers(); } ///  /// Modifiers list from database ///  ///  private Modifier[] GetModifiers() { List modifier = new List(); ModifierCollection matchingModifiers = ModifierList.GetModifierList(); foreach (Modifier ModifierRow in matchingModifiers) { modifier.Add(new Modifier(ModifierRow.ModifierCode, ModifierRow.Description)); } return modifier.ToArray(); } } 

目标代码:

  public static class ModifierList { ///  /// Returns the Modifier Collection. ///  ///  ///  public static ModifierCollection GetModifierList() { 

今天我一直在与一个与.Net Web服务交谈的iPhone应用程序进行斗争。

我发现,如果我将我的内容类型更改为application / jsonrequest,它会毫无问题地完成,并且我能够在我的Web服务器中处理数据。

只是为了笑容,我将上面提到的行添加到我的web.config中,但它没有使application / json工作。

不确定它是否可以这么简单,但我使用jQuery从我的Web方法回调JSON。

我看到的主要区别是类的属性

[System.Web.Script.Services.ScriptService]

  [WebService(Namespace = "http://MyNameSpace")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class Web : System.Web.Services.WebService{ [WebMethod] public string TestMethod(){ return "Testing"; } } 

我必须假设您使用的是3.5框架,因为这是公开JSON Web方法的唯一方法。

我的调用formsjQuery看起来几乎相同,所以没有问题。

如果您在IE中测试它,请尝试从您的contentType属性中删除charset声明(即它应如下所示:

 contentType: "application/json", 

我还没有发现原因,但是当使用“ charset=UTF-8 ”部分进行JSON调用时,IE似乎有点扭曲。

替代文字

我认为您正在寻找WebInvoke或WebGet属性,它允许您指定Uri模板,正文样式,请求和响应格式,例如:

 [WebGet(ResponseFormat= WebMessageFormat.Json)] 

此链接可能有所帮助。 WebInvoke有一篇类似的文章(主要用于post)。

我使用JQuery AJAX JSON调用ASMX webservice相当多。 它适用于所有浏览器。 我正在使用安装了ASP.NET AJAX Extensions的.NET 2.0(捆绑在3.5中)。

我的class级与你的class级有相同的装饰。 我的方法只有[WebMethod(EnableSession = true)]装饰器。 但是我的web.config在其httpHandlers部分中有以下条目:

  

我的jquery调用如下:

 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "path/to/service/jsonservice.asmx/MethodName", data: JSON.stringify(DTO), dataType: "json", success: function(rtrn) { alert("good"); }, error: function(req, msg, err) { alert("bad"); } }); 

这篇文章是我的知识的根源。

看起来您必须在scriptMethod标记中指定json作为响应格式。 这是来自vb.net,但我相信你明白了这个想法:

ResponseFormat:= ResponseFormat.Json