无法在没有任何数据的情况下通过JQuery调用ASPX函数
我试图使用jquery调用我的aspx函数,但我无法调用它。我已经在代码中添加了一个断点,但该断点从未关闭,这意味着从未调用该函数。以下是我的代码: jquery:无法在没有任何数据的情况下通过JQuery调用ASPX函数,jquery,asp.net,function,call,Jquery,Asp.net,Function,Call,我试图使用jquery调用我的aspx函数,但我无法调用它。我已经在代码中添加了一个断点,但该断点从未关闭,这意味着从未调用该函数。以下是我的代码: jquery: update(); function update() { $.ajax({ url: 'Codes.aspx/testfunction', method: 'post', success: function () { alert("yay"); }, fai
update();
function update() {
$.ajax({
url: 'Codes.aspx/testfunction',
method: 'post',
success: function () { alert("yay"); },
failure: function () { alert("no"); }
});
}
aspx:
始终调用success函数,但代码从未在断点处中断
但是,我希望在aspx函数中用db填充数据
让我知道我错在哪里
整个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>
$(文档).ready(函数(){
$(“#添加_代码”)。单击(函数(){//空代码验证
更新();
返回false;
});
});
函数更新(){
var obj={};
obj.response=“从更新功能发送”;
$.ajax({
url:“default2.aspx/testfunction”,
方法:“张贴”,
数据:JSON.stringify(obj),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
//async:true,
//cache:false,
成功:函数(response){alert(response.d);},
失败:函数(响应){alert(“no”+response.d);}
});
}
好吧,ASPX函数不起作用,所以我创建了一个新的Web服务并从那里调用它。它运作顺利。感谢您的时间和解释。:) 你能尝试从“testfunction”中删除“static”关键字吗?@ajawad987我尝试过,但没有成功。我已经在testfunction之前添加了这个web方法。还是不行/按钮>关闭是否在文档中调用此Javascript函数。准备好了吗??或者把它绑在按钮上。我没有看到用于投标的代码。我是从脚本中的document ready函数内部调用它的。我尝试了你的方法,但我总是在javascript弹出窗口中未定义。
<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>