2010-07-12 71 views
0

我试着运行sipmle ASP.NET 2 WEBMETHOD用JSON 这是我的代码:JSON未定义parsererror

<head> 
<title></title> 
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.3.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 

     $("#btGetDate").click(function() { 
      $.ajax({ 
       type: "POST", 
       url: "GetDate.asmx/HelloWorld", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 
        alert(0); 
       }, 
       error: 
       function (XMLHttpRequest, textStatus, errorThrown) { 
        $('div#dvDate').html(errorThrown + textStatus); 
       } 
      }); 

     }); 
    }); 
</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<div id="dvDate"></div><input id="btGetDate" type="button" value="Get Date" /> 
</div> 
</form> 
</body> 
</html> 

和web服务

namespace AJAX_METHODS 
{ 
    /// <summary> 
    /// Summary description for GetDate 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 

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

     [WebMethod] 
     public string HelloWorld() 
     { 
      return "Hello World"; 
     } 

     [WebMethod] 
     public string GetDateTime() 
     { 
      return DateTime.Now.ToString(); 
     } 
    } 
} 

我得到一个解析错误回来,没有想法为什么。

感谢您的答案。

+0

您是否尝试过使用Firebug的或类似的应用程序/插件分析网络流量?如果没有,我推荐它。 – 2010-07-12 08:37:05

回答

3

你忘了,表明该服务应该通过与[ScriptService]属性装饰它返回JSON:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[ScriptService] 
public class GetDate : System.Web.Services.WebService