Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 虽然返回格式为json,但返回xml的web服务_C#_Jquery_Ajax_Web Services - Fatal编程技术网

C# 虽然返回格式为json,但返回xml的web服务

C# 虽然返回格式为json,但返回xml的web服务,c#,jquery,ajax,web-services,C#,Jquery,Ajax,Web Services,这是web服务 public class HelloWorld : System.Web.Services.WebService { [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string Hello() { return "Hello World"; } } 下面是javascript ajax代码 $(document).read

这是web服务

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 
Excep::无效的JSON:
你好,WorldStatus::好的
尝试使用

[ScriptMethod(UseHttpPost=true, ResponseFormat=ResponseFormat.Json)]
还可以从您的服务返回一个对象,以便客户端可以更可预测地使用它

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

确保您在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>

编辑

我建议您浏览以下Dave Ward的博客文章,讨论安装和配置错误: