【问题标题】:web service returning xml although return format is jsonWeb 服务返回 xml,虽然返回格式是 json
【发布时间】:2013-07-10 11:34:35
【问题描述】:

这里是网络服务

public class HelloWorld : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Hello()
    {
        return "Hello World";
    }
}

这里是 javascript ajax 代码

 $(document).ready(function () {
        $.ajax({
            url: "Service/HelloWorld.asmx/Hello",
            dataType: 'json',
            type: 'POST',
            cache: false,
            crossDomain: true,
            timeout: 15000,
            success: function (rtndata) {
                alert('Success : :' + rtndata);
            },
            error: function (xhr, errorType, exception) {
                alert("Excep:: " + exception + "Status:: " + xhr.statusText);
            }
        });
    });

遇到错误

    Excep:: Invalid JSON: <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>Status:: OK 

【问题讨论】:

    标签: c# jquery ajax web-services


    【解决方案1】:

    确保您在 web.config 的 Handler 部分下设置了 ScriptHandlerFactory。

    <httpHandlers>
         <remove verb="*" path="*.asmx"/>
         <add verb="*" path="*.asmx" validate="false"
              type="System.Web.Script.Services.ScriptHandlerFactory,
              System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
              PublicKeyToken=31bf3856ad364e35"/>
    </httpHandlers>
    

    欲了解更多信息,请查看以下链接:

    http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-web-services

    编辑

    我建议您阅读 Dave Ward 的以下博客文章,其中讨论了安装和配置错误:

    http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/

    【讨论】:

      【解决方案2】:

      尝试使用

      [ScriptMethod(UseHttpPost=true, ResponseFormat=ResponseFormat.Json)]
      

      还可以从您的服务返回一个对象,以便客户端可以更可预测地使用它。

      return new Result() { Value = "some string" };
      

      【讨论】:

        猜你喜欢
        • 2023-03-22
        • 1970-01-01
        • 2012-02-26
        • 2023-04-05
        • 1970-01-01
        • 2018-04-22
        • 1970-01-01
        • 2012-06-20
        相关资源
        最近更新 更多