【问题标题】:JSON and ASP.NET WebMethod not firing server side methodJSON 和 ASP.NET WebMethod 不触发服务器端方法
【发布时间】:2020-02-18 20:43:30
【问题描述】:

我正在尝试使用 WebMethod 将参数传递给我的代码。

我已达到 ajax 的成功结束,但我背后的 aspx.cs 代码中的方法没有被调用并且我有错误。

操作失败!详情:'[object 对象]

如果有影响,我会使用母版页。

如何解决这个问题?

拜托,你能帮帮我吗?

Javascript:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=imgfasi]").bind("click", function () {
            var fasi = {};
            fasi.Txseltlc = $("[id*=txseltlc]").val();
            fasi.Txrescldisa = $("[id*=txrescldisa]").val();
            fasi.Ddlauttlc = $("[id*=ddlauttlc]").val();
            $.ajax({
                type: "POST",
                url: "Default.aspx/Savepnfasi",
                data: '{fasi: ' + JSON.stringify(fasi) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response == "1") {
                        alert("Success!");
                    } else {
                        alert("Operation failed! Details: " + response);
                    }
                }
            });
            return false;
        });
    });
</script>

后面的代码:

public class pnfasiweb
{
    public string Txseltlc { get; set; }
    public string Txrescldisa { get; set; }
    public string Ddlauttlc { get; set; }
}

[WebMethod]
[ScriptMethod]
public static void Savepnfasi(pnfasiweb fasi)
{
    if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
    {         
        string ProductID = Mpskmt3.Base64ForUrlDecode(HttpContext.Current.Request.QueryString["id"].ToString());

        string sql = String.Format(@" UPDATE `dotable` ");
        sql += String.Format(" SET ");
        sql += String.Format(" Aut = ?, ");
        sql += String.Format(" Res = ?, ");
        sql += String.Format(" Dur = ?, ");
        sql += String.Format(" Comp = CASE WHEN Comp IS NULL THEN ? ELSE CONCAT(Comp, '; ', ?) END, ");
        sql += String.Format(" doDateHour = CURRENT_TIMESTAMP() ");
        sql += String.Format(" WHERE ID = ?; ");

        using (OdbcConnection cn =
                new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQLlocalhost"].ConnectionString))
        {
            using (OdbcCommand command =
                    new OdbcCommand(sql, cn))
            {
                try
                {
                    command.Connection.Open();                       
                    command.Parameters.AddWithValue("param1", fasi.Ddlauttlc.ToString());
                    command.Parameters.AddWithValue("param2", Convert.ToInt32(fasi.Txrescldisa.ToString()));
                    command.Parameters.AddWithValue("param3", Convert.ToInt32(fasi.Txseltlc.ToString()));
                    command.Parameters.AddWithValue("param4", Mpskmt3.Container.TheObjectPropertyName);
                    command.Parameters.AddWithValue("param5", Mpskmt3.Container.TheObjectPropertyName);
                    command.Parameters.AddWithValue("param6", ProductID.ToString());
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    command.Connection.Close();
                }
            }
        }
    }
    else
    {
       //Error
    }
}

#Edit01

【问题讨论】:

  • 调试时的一些提示:不要抛出 ex;,只需 throw;。这样你就可以堆栈跟踪。不要提醒你的错误,console.log 它们。在 javascript 中设置断点并在 chrome 控制台中进行调试。同样对于警报对象,不要将字符串连接到它们。最后,如果您想在客户端清楚地看到您的消息,请尝试使用response.data
  • @JeffLi 谢谢,我已经添加了response.data 并且在输出中我有正确的值。请在我的第一个问题中查看我的 #Edit01
  • 为ajax写一个error:function(e){ alert(e); },你可以找到它是什么错误
  • @VidiyaPrasanth 谢谢,但是什么时候写你的建议?
  • @VidiyaPrasanth 问题是 string ProductID = Mpskmt3.Base64ForUrlDecode(HttpContext.Current.Request.QueryString["id"].ToString()); 因为 JSON 使用 POST 方法,我尝试使用 QueryString 方法更新查询...请你帮帮我吗?

标签: javascript c# json webmethod


【解决方案1】:

试试这个:

var qString = "?" + window.location.href.split("?")[1];
var fasi = {};

...

url: "Default.aspx/Savepnfasi" + qString,
data: '{fasi: ' + JSON.stringify(fasi) + '}',

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多