gpt4 book ai didi

javascript - JSON 和 ASP.NET WebMethod 不触发服务器端方法

转载 作者:行者123 更新时间:2023-12-01 00:07:22 25 4
gpt4 key购买 nike

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

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

Operation failed! Details :'[object 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

enter image description here

最佳答案

试试这个:

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

...

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

关于javascript - JSON 和 ASP.NET WebMethod 不触发服务器端方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60289151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com