Webservice的忽略ResponseFormat.Json [英] Webservice ignores ResponseFormat.Json

查看:1102
本文介绍了Webservice的忽略ResponseFormat.Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个简单的$就张贴到我的asp.net web服务,返回JSON。我已经尝试了所有可能的解决方案在那里,没有任何运气。我缺少的东西吗?

I want to do a simple $.ajax POST to my asp.net Webservice, returning Json. I've tried all the possible solutions out there, without any luck. Am I missing something?

我的jQuery的code:

My jQuery code:

$.ajax({
    type: "POST",
    url: "/Services/ConfiguratorService.asmx/GetInitialJson",
    contentType: "application/json; charset=utf-8",
    data: "{}",
    dataType: "json",
    success: function (data, textStatus, jqXhr) {
        if (data != null) {
            alert(data.d);
        } else {
            alert("data undefined: | " + jqXhr + " | " + textStatus);
        }
    },
    error: function (jqXhr, textStatus, errorThrown) {
        alert(jqXhr + " | " + textStatus + " | " + errorThrown);
    }
});

我asmx.cs code:

My asmx.cs code:

[WebMethod(Description = "Gets initial configuration values for Configurator")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public ConfiguratorModel GetInitialModel()
{
    return new Item { Title: "10 units", Text: "Item text..." };
}
public class Item { public string Title {get; set;} public string Text {get; set;} }

更新1:
在我的解决方案,我收到回一个响应,但只作为XML。我试图重现错误在一个新的解决方案,没有运气。一切工作就好了。在新旧解决方案的.asmx服务实际上将返回相同的响应。唯一的区别是,jQuery的在其中的一个失败,错误:parsererror,意外令牌LT;

Update 1: In my solution, I'm getting back a response, but only as XML. I've tried to reproduce the error in a fresh solution, with no luck. Everything works just fine. .asmx services in both old and new solution actually return the same response. The only difference is that jquery in one of them fails with error: parsererror, unexpected token <

更新2:
在我以前的解决方案的响应头始终为text / xml的;字符集= UTF-8。

Update 2: Response header in my old solution is always "text/xml; charset=utf-8".

更新3:
我的web.config拥有所有需要的条目。回应仍是文本/ XML的,字符集= UTF-8。为什么呢?

Update 3: My web.config has all the entries that are required. Response is still "text/xml; charset=utf-8". Why?

推荐答案

我的一位同事帮了我,在方向指向。我发现,处理程序是asp.net 2.0。将溶液从今年2.0-4.0更早升级。我所要做的就是替换:

A colleague of mine helped me out, pointing in direction. I found out that handlers were asp.net 2.0. The solution was upgraded from 2.0 to 4.0 earlier this year. all I had to do was replace:

<add name="WebServiceHandlerFactory-Integrated" path="*.asmx" verb="GET,HEAD,POST,DEBUG" type="System.Web.Services.Protocols.WebServiceHadlerFactory, System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode" responseBufferLimit="4194304" />

<add name="WebServiceHandlerFactory-Integrated-4.0" path="*.asmx" verb="GET,HEAD,POST,DEBUG" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" preCondition="integratedMode,runtimeVersionv4.0" />

好主意是尝试暂时删除所有处理程序,只增加:

Good idea is to try removing all of the handlers temporarily, adding only:

<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

这篇关于Webservice的忽略ResponseFormat.Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆