JSON未定义parsererror

时间:2010-07-12 08:33:44

标签: jquery asp.net-2.0

我试图用JSON运行一个sipmle ASP.NET 2 Webmethod 这是我的代码:

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

和网络服务

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();
        }
    }
}

我得到一个解析错误,不明白为什么。

感谢您的回答。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

裸字符串无效JSON。请参阅json.org