在没有任何数据的情况下无法通过 JQuery 调用 ASPX 函数
Unable to call ASPX function through JQuery without any data
我正在尝试使用 jquery 调用我的 aspx 函数,但我无法调用它。我在我的代码中添加了一个断点,但该断点永远不会消失,这意味着该函数永远不会被调用。以下是我的代码:
jquery:
update();
function update() {
$.ajax({
url: 'Codes.aspx/testfunction',
method: 'post',
success: function () { alert("yay"); },
failure: function () { alert("no"); }
});
}
aspx:
public static void testfunction()
{
// A break point in visual studio here
}
success 函数总是被调用,但代码永远不会在断点处中断。
但是,我希望在 aspx 函数中将我的数据填充到数据库中。
让我知道哪里错了。
整个Jquery函数:
$(document).ready(function () {
$("#Add_Code").click(function () { //Empty code validation
var new_code = $("#<%=new_code.ClientID%>").val();
// alert(new_code);
if (new_code == "") {
$("#validation_code").show();
$("#error_div").addClass("has-error");
}
else if (new_code != "") {
update();
function update() {
$.ajax({
url: "Codes.aspx/testfunction",
method: "POST",
data: "Sent from update function",
contentType: "application/json; charset=utf-8",
dataType: "json",
//async: true,
//cache: false,
success: function (response) { alert(response.d); },
failure: function (response) { alert("no "+response.d); }
});
return false;
}
}
})
更新代码:
[WebMethod]
public static string testfunction(string response)
{
string abc = response;
return abc + "and server";
}
将其粘贴到脚本标签中。添加用于检查空值的 if 条件
<script>
$(document).ready(function () {
$("#Add_Code").click(function () { //Empty code validation
update();
return false;
});
});
function update() {
var obj = {};
obj.response = "Sent from update Function";
$.ajax({
url: "default2.aspx/testfunction",
method: "POST",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
//async: true,
//cache: false,
success: function (response) { alert(response.d); },
failure: function (response) { alert("no " + response.d); }
});
}
</script>
好吧,ASPX 功能不起作用,所以我创建了一个新的 Web 服务并从那里调用它。它工作顺利。谢谢 Gagan Deep 的时间和解释。 :)
我正在尝试使用 jquery 调用我的 aspx 函数,但我无法调用它。我在我的代码中添加了一个断点,但该断点永远不会消失,这意味着该函数永远不会被调用。以下是我的代码:
jquery:
update();
function update() {
$.ajax({
url: 'Codes.aspx/testfunction',
method: 'post',
success: function () { alert("yay"); },
failure: function () { alert("no"); }
});
}
aspx:
public static void testfunction()
{
// A break point in visual studio here
}
success 函数总是被调用,但代码永远不会在断点处中断。
但是,我希望在 aspx 函数中将我的数据填充到数据库中。
让我知道哪里错了。
整个Jquery函数:
$(document).ready(function () {
$("#Add_Code").click(function () { //Empty code validation
var new_code = $("#<%=new_code.ClientID%>").val();
// alert(new_code);
if (new_code == "") {
$("#validation_code").show();
$("#error_div").addClass("has-error");
}
else if (new_code != "") {
update();
function update() {
$.ajax({
url: "Codes.aspx/testfunction",
method: "POST",
data: "Sent from update function",
contentType: "application/json; charset=utf-8",
dataType: "json",
//async: true,
//cache: false,
success: function (response) { alert(response.d); },
failure: function (response) { alert("no "+response.d); }
});
return false;
}
}
})
更新代码:
[WebMethod]
public static string testfunction(string response)
{
string abc = response;
return abc + "and server";
}
将其粘贴到脚本标签中。添加用于检查空值的 if 条件
<script>
$(document).ready(function () {
$("#Add_Code").click(function () { //Empty code validation
update();
return false;
});
});
function update() {
var obj = {};
obj.response = "Sent from update Function";
$.ajax({
url: "default2.aspx/testfunction",
method: "POST",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
//async: true,
//cache: false,
success: function (response) { alert(response.d); },
failure: function (response) { alert("no " + response.d); }
});
}
</script>
好吧,ASPX 功能不起作用,所以我创建了一个新的 Web 服务并从那里调用它。它工作顺利。谢谢 Gagan Deep 的时间和解释。 :)