Webservice忽略ResponseFormat.Json

时间:2013-06-21 13:03:05

标签: c# jquery asp.net

我想对我的asp.net Webservice做一个简单的$ .ajax POST,返回Json。我已经尝试了所有可能的解决方案,没有任何运气。我错过了什么吗?

我的jQuery代码:

$.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代码:

[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,意外标记<

更新2: 旧解决方案中的响应标头始终为“text / xml; charset = utf-8”。

更新3: 我的web.config包含了所有必需的条目。响应仍然是“text / xml; charset = utf-8”。为什么呢?

3 个答案:

答案 0 :(得分:1)

我的一位同事帮我指出了方向。我发现处理程序是asp.net 2.0。该解决方案今年早些时候从2.0升级到4.0。我所要做的就是替换:

<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" />

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

<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"/>

答案 1 :(得分:0)

使用static关键字来调用方法,因为您必须定义静态方法

public static ConfiguratorModel GetInitialModel()
{
    return new Item { Title: "10 units", Text: "Item text..." };
}

答案 2 :(得分:0)

我有这样的网络服务:

[WebService(Namespace = "http://example.com/")]
[System.Web.Script.Services.ScriptService]
public class api : System.Web.Services.WebService {
    [WebMethod(EnableSession = true)]
    public ServiceResponse<string> SignIn(string _email, string _password) {...}
}

和$ .ajax跟你的打电话一样。一切都很好,对我来说。 您可能需要向服务类添加[System.Web.Script.Services.ScriptService]。据我记忆,这是JSON响应的关键。 jQuery调用是这样的:

$.ajax({
      type: "POST",
      url: "/api.asmx/SignIn",
      dataType: "json",
      data: JSON.stringify({"_password": pwd,
                            "_email": email
                        }),
      contentType: 'application/json; charset=utf-8',
      success: function (res) { }
   });

修改

public ConfiguratorModel GetInitialModel()
{
    return new Item { Title: "10 units", Text: "Item text..." };
}

此方法定义看起来不对。当返回类型为Item时,我看不到您如何返回ConfiguratorModel。这必须在编译时失败。