C# Webservice忽略ResponseFormat.Json

C# Webservice忽略ResponseFormat.Json,c#,jquery,asp.net,C#,Jquery,Asp.net,我想向我的asp.net Web服务发送一篇简单的$.ajax文章,返回Json。我已经尝试了所有可能的解决方案,但没有任何运气。我错过什么了吗 我的jQuery代码: $.ajax({ type: "POST", url: "/Services/ConfiguratorService.asmx/GetInitialJson", contentType: "application/json; charset=utf-8", data: "{}", data

我想向我的asp.net Web服务发送一篇简单的$.ajax文章,返回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失败并出现错误:parserrror,意外标记使用
static
关键字调用方法,就像您定义static方法一样

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

使用
static
关键字调用方法,就像您定义静态方法一样

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

我有这样的网络服务:

[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) {...}
}
编辑:

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

此方法定义看起来是错误的。当返回类型为
ConfiguratorModel
时,我看不出您如何返回
。编译时一定会失败。

我有这样的web服务:

[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) {...}
}
编辑:

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

此方法定义看起来是错误的。当返回类型为
ConfiguratorModel
时,我看不出您如何返回
。编译时一定会失败。

我的一位同事帮我解决了这个问题,为我指明了方向。我发现处理程序是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="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"/>

我的一位同事帮我指出了方向。我发现处理程序是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="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"/>


您得到了什么?错误?还是别的什么?请说明我得到的模型是text/xml,而不是jquery中指定的application/json。你得到的是什么?错误?还是别的什么?请指定我获取的模型是text/xml,而不是jquery中指定的application/json。服务将返回模型。将其设为静态也无济于事。服务正在返回模型。让它保持静止也无济于事。会有什么反应?它是否包含它应该包含的内容,但只是XML格式的?正确,模型返回得很好,只是作为text/XML。我在新的解决方案中测试了我的代码和web.config配置。返回的是json。似乎我的另一个解决方案中的一些旧配置弄乱了我的响应,我只是不知道是哪一个:/您可以尝试从新解决方案中复制web.config,并查看它是否与旧解决方案兼容。就是这样。。。是asp.net 2.0,而不是4.0。你能发一个回复吗,我会把它标记为“答案”你找到了你自己问题的答案,所以把它像答案一样发出来没问题)你的回答是什么?它是否包含它应该包含的内容,但只是XML格式的?正确,模型返回得很好,只是作为text/XML。我在新的解决方案中测试了我的代码和web.config配置。返回的是json。似乎我的另一个解决方案中的一些旧配置弄乱了我的响应,我只是不知道是哪一个:/您可以尝试从新解决方案中复制web.config,并查看它是否与旧解决方案兼容。就是这样。。。是asp.net 2.0,而不是4.0。你能发一个回复吗,我会把它标记为“答案”你找到了你自己问题的答案,所以把它当作答案发出来没有问题)