0

我正在尝试在启动 Metro UI 应用程序之前返回 json 数据以使用它。我的网络服务不返回 json 数据,但它总是返回 xml 数据。有人可以帮助我吗?

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

网页

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
    <title>Untitled Page</title> 
    <script language="javascript" type="text/javascript">

        var req = new XMLHttpRequest();
        xmlhttp.setRequestHeader("Content-Type", "application/json"); 

        function Roll() {
            if (req.readyState == 4) {
                alert(req.responseText);
                alert("exiting");
            }
        }

        function Test() {
            alert("starting");
            req.open("POST", "http://localhost:58718/testService/Service.asmx/HelloWorld", true);
            req.onreadystatechange = Roll;
            req.send(null);
        } 


    </script> 
</head> 
<body> 

    <button onclick="javascript:Test()">TEST</button> 
</body> 
</html>
4

1 回答 1

0

将此添加到您的 Web 配置文件中。

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>

信仰斯隆

于 2012-05-31T22:12:59.330 回答