0

我尝试使用 jQuery 实现 ajax。这是我第一次使用 ajax.so 在按钮单击中我需要显示当前日期时间。为此,我编写了以下代码

//==============aspx页面===============

<script type="text/javascript">

    $(document).ready(function (){
        var Button1 = $("#Button1");
        var Label1 = $("#Label1");
        Button1.click(function (){
          $.ajax({
                type: "POST",
                url: "/myownajax14/WebService/WebService1.asmx/GetDateTime",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#Label1").text(msg.d);
                    //alert('hi');
                    },
                error:alert('error');     
            });     

        });
    });

//================asmx.cs页面===========

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod]
    public string GetDateTime()
    {
        return DateTime.Now.ToString();
    }
}

我的问题是,它没有给出答案..它没有显示任何错误。这段代码有任何错误吗?请帮助我..

4

3 回答 3

0

您是否尝试过使用浏览器直接访问 ASMX?这样,您可以轻松查看您的 ASMX 正在产生什么样的响应......

于 2013-02-18T07:09:10.647 回答
0

我也有类似的问题,很简单试试这个:

您需要指定type="button"属性。

于 2013-02-18T05:14:02.697 回答
0

内容类型:“应用程序/json;” 表示客户端需要服务器响应一个json类型的数据,但是服务器响应的是一个字符串类型的数据。

在“成功”块中,“msg”必须像“2010-10-10”,所以在 msg.d 中会发生错误。

尝试以下字符串类型数据,或在服务器代码中响应 json 类型数据,例如 {"y":2010,"m":10,"d":10}。

contentType: "application/text; charset=utf-8",
success: function (date) {
    $("#Label1").text(date);
}
于 2013-02-18T05:22:27.293 回答