我想从aspx页面调用一个c#函数,我在下面试过了
function DeleteKartItems(callback) {
$.ajax({
type: "POST",
url: 'About.aspx/updatingdatabase',// my function name in c#
data: '{"username":"' + col1 + '","password":"' + col2 + '","age":"' + col3 + '","city":"' + col4 + '","id":"' + idlast + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var x = data.d;// i am trying to store the return data into a local variable
},
error: function (e) {
}
});
}
我的问题是,当我把c函数写成静态函数时,它工作得很好,但从其他方面来说,它不会工作,我想知道有没有任何方法可以从aspx页面调用非静态的c函数提前感谢
不可能直接通过url从aspx页面运行函数。
尝试以下操作:
-
更改您的ajax请求如下:
$.ajax({ type: "POST", url: 'About.aspx', data: '{"method":"updatingdatabase","username":"' + col1 + '","password":"' + col2 + '","age":"' + col3 + '","city":"' + col4 + '","id":"' + idlast + '"}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { var x = data.d;// i am trying to store the return data into a local variable }, error: function (e) { } });
-
更新页面中的
Page_Load
处理程序:protected void Page_Load(object sender, EventArgs e) { if (!String.IsNullOrEmpty(Request["method"]) && String.Compare(Request["method"], "updatingdatabase", true) == 0) { UpdatingDatabase(); //run the method } }
尝试这个
data: '{"method":"updatingdatabase","username":"' + col1 + '","password":"' + col2 + '","age":"' + col3 + '","city":"' + col4 + '","id":"' + idlast + '"}',
而不是
url: 'About.aspx',
data: '{"method":"updatingdatabase","username":"' + col1 + '","password":"' + col2 + '","age":"' + col3 + '","city":"' + col4 + '","id":"' + idlast + '"}',
和pageload 中的调用函数