【问题标题】:JQuery Method control goes to success block but msg.d showing UndefinedJQuery 方法控制进入成功块,但 msg.d 显示未定义
【发布时间】:2012-12-07 11:56:25
【问题描述】:

我在 .net1.1 onload 的 body 中调用这个 javascript 方法“test”。我的 webmethod 返回字符串数据,但我无法在我的 Jquery 方法中获取该数据。 在 HiddenPage.aspx 中 ===============================

函数测试() {

       debugger;

$.ajax({
                type: "POST",
                url: "HiddenPage.aspx/GetServerTime",
                //async : false,
                //data: "i=1",
                contentType: "application/json",
                //dataType: "text",
                success: function(msg) 
                // error: function(, textStatus, errorThrown) {
                {
                    debugger;
                         alert(msg.d);

                }, 
                error: function(msg) 
                //complete: function (jqXHR, textStatus) {

                {
                   debugger;
                   alert(msg.d); 
                    alert("Error! Try again..."); 
                    //return false;
                }


            })
     // return '';
  }

在 HiddenPage.aspx.cs 我放了 webmthod.My WebMethod 是:-

    [WebMethod()]
    public static string GetServerTime()
    {

        return DateTime.Now.ToString();
    }

【问题讨论】:

  • 贴出GetServerTime()的代码
  • @giftcv,我已添加。请检查我编辑的代码..谢谢

标签: c# asp.net jquery-ui jquery


【解决方案1】:

能否请您发布返回数据的代码。

我建议您创建一个 ASMX 文件来使用 Web 服务。它很容易使用。 创建一个 web 服务,然后确保在 web 方法之前在 web 服务中添加以下行。

[System.Web.Script.Services.ScriptService]

之后,您可以添加与您编写的相同的 Web 方法。

你的jquery应该是这样的。

  $.ajax({
            type: "POST",
            url: "webservice/WebService1.asmx/GetServerTime",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccessCall,
            error: OnErrorCall
        });        

    function OnSuccessCall(msg) {

        alert(msg.d);


    }

    function OnErrorCall(msg) {
        alert(msg.status + " " + msg.statusText);
    }

它可以帮助你。编码愉快。

【讨论】:

    【解决方案2】:

    不太确定您的返回数据是什么样的,但您可以尝试以下方法。

    $.ajax({
        type: "POST",
        url: "HiddenPage.aspx/GetServerTime",
        //async : false,
        //data: "i=1",
        contentType: "application/json",
        dataType: "html",
        success: function(data){
            alert(data);
        },
        error: function(jqXHR, textStatus) {
            debugger;
            if (jqXHR.status === 0) alert('Not connect.\n Verify Network.');
            else if (jqXHR.status == 404) alert('Requested page not found. [404]');
            else if (jqXHR.status == 500) alert('Internal Server Error [500].');
            else if (textStatus === 'parsererror') alert('Requested JSON parse failed.');
            else if (textStatus === 'timeout') alert('Time out error.');
            else if (textStatus === 'abort') alert('Ajax request aborted.');
            else alert('Uncaught Error.\n' + jqXHR.responseText);
            //return false;
        }
        //return '';
    }​
    

    【讨论】:

    • 返回数据只是一个字符串,检查我编辑的问题,控制进入成功块..谢谢
    【解决方案3】:

    试试下面的

    $.ajax({
      type: "POST",
      url: "HiddenPage.aspx/GetServerTime",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
                alert(msg.d);
        // Do something interesting here.
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 2023-02-21
      • 2015-12-12
      • 1970-01-01
      • 2015-03-03
      相关资源
      最近更新 更多