【问题标题】:Error function called when I parse the parameters using jquery AJAX function to asp.net behind code当我使用 jquery AJAX 函数将参数解析为 asp.net 代码时调用的错误函数
【发布时间】:2018-10-25 12:01:06
【问题描述】:

这是我的aspx页面代码

function grid(datas)
{
    var datass = {datas: datas};
    var myJSON = JSON.stringify(datass);

    $.ajax({
        url: 'EditCustomerBills.aspx/LoadGrid',
        method: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: myJSON,
        success: function (data) { 
           alert("Success");
        },
        error:function(error){
            alert("failed");
        }         

    });
}

这是我的后台代码

[WebMethod]
public static string LoadGrid(string datas)
{
    //GetCustomerBillDetail(data);
    string datass = datas.ToString();
    return datass;
}

我正在完美地使用代码,但输出失败。我挣扎了四天。请帮我。谢谢。

【问题讨论】:

  • 错误是什么?
  • 通常你不需要在ajax的URL中添加你所在的aspx,你可以简单地做/LoadGrid它应该可以工作。如果您只是向我们提供错误消息,也会有很大帮助。

标签: javascript jquery asp.net ajax


【解决方案1】:

我不知道你的“myJSON”是什么。根据我的说法,你的代码应该是这样的。

 [System.Web.Services.WebMethod]
 public static string GetCurrentTime(string datas)
    {
        return "Hello " + datas + Environment.NewLine + "The Current Time is: "
            + DateTime.Now.ToString();
    }


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowCurrentTime() {
    $.ajax({
        type: "POST",
        url: "CS.aspx/GetCurrentTime",
        data: '{datas: "Your Data" }', // your data should be here.
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>

下图会让你了解我们如何调用服务器端方法。

更多参考请见this

【讨论】:

    【解决方案2】:

    如下编辑并在控制台输出中与 f12 共享?

    error: function (error) {
                    console.log(error);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-23
      • 2011-12-21
      • 1970-01-01
      • 2013-01-13
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      相关资源
      最近更新 更多