2012-05-31 68 views
0

我想在我啓動我的metro UI應用程序之前返回json數據以使用它。 我的web服務不返回json數據,但它總是重新調用xml數據。有人可以幫我解決這個問題。Web服務不返回json數據

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

HTML頁面

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

回答

0

添加到您的Web配置文件。

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

信仰斯隆

+0

這些線路是否有默認情況下在我的web.config文件。我正在使用框架版本3.5。 – Kurkula