[英]calling function in the javascript written in webpage(.cs)
我在.cs页面上具有功能
[System.Web.Services.WebMethod]
public static string getdata()
{
ProductBAL objbal = new ProductBAL(); // Calling class
int i = 0;
i = objbal.get_last_orderid(); //Select query
i = i + 1;
ProductDAL objdal = new ProductDAL(); // Calling class
objdal.insert_new_orderid(i); //Insert query
HttpCookie orderid = new HttpCookie("orderid");
orderid.Value = "MP_" + Convert.ToString(i);
Response.Cookies.Add(orderid);
Response.Cookies["orderid"].Expires = DateTime.Now.AddHours(5);
string abc=Convert.ToString(i);
return abc;
}
我的HTML页面代码是
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>calling function from .cs</title>
<script language="javascript" type="text/javascript">
function Submit1_onclick() {
$.ajax({ type: "GET", url: "default.aspx/getdata()", success: function (data) { });
alert("Done");
}
</script>
</head>
<body>
<form name="ecom" method="post" action="https://www.google.co.in/">
<input id="Submit1" type="submit" name="submit" runat="server" value="Submit" onclick="return Submit1_onclick()">
</form>
</body>
我试图在提交点击时将我的Web端函数调用给客户端。 我想念什么吗? 请从我上面的代码中进行演示
function Submit1_onclick() {
// alert("Hello");
$.ajax({
type: "GET",
url: 'demo.aspx/getdata',
data: "{}",
//"{character:'M'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data.d);
//alert("success");
alert("This is ajax call:");
},
error: function() {
//alert(Error);
alert("something went wrong");
}
});
// alert("Done");
}
[WebMethod()] //U have to declare this method as a web method
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string getdata()
{
在您的网址上尝试:“ PageName.aspx / MethodName”。 另请参阅Dave Ward的这篇博客文章:
下面的行容易出错。 网址方法名称中不要包含“()”。
$.ajax({ type: "GET", url: "/getdata()", success: function (data) { });
将上面的行替换为
$.ajax({ type: "GET", url: "/getdata", success: function (data) { });
请参阅以下工作示例
// Code behind method declared static
[WebMethod]
public static string GetSquare(String value)
{
return "hello" + value;
}
您的按钮,必须单击此按钮
<input type="button" id="button" value="Chnageurl" onclick="ajaxcall()" />
为此脚本
<script type="text/jscript">
function ajaxcall(e) {
$.ajax({
type: "POST",
url: "Default.aspx/GetSquare",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ value: "test" }),
dataType: "json",
success: function (value) {
alert(value.d);
},
error: function () { alert("Ajax Error"); }
});
};
从您的评论中,我收集到您正在通过检查数据库表中的新条目来验证此方法是否有效。 数据库中的数据可能由于其他原因而不是查询而丢失。 要进行验证,请尝试使用更简单的网络方法,然后从那里开始。
例如,
HTML:
<input id="submit" type="submit" name="submit" value="Submit" onclick="return submitClick();" />
Javascript:
function submitClick() {
$.ajax({
type: "POST",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "default.aspx/getdata",
success: function (data) {
console.log(data);
alert("success" + data);
},
error: function () {
alert("something went wrong");
}
});
return false; // Note: the return false will prevent postback
}
C#
[System.Web.Services.WebMethod]
public static string getdata()
{
return "Hello World!";
}
如果您没有看到成功的响应,则问题确实出在您的javascript上,或者是网站设置,从而以某种方式阻止了javascript的回调。
如果该方法成功,则您的数据库插入脚本可能会引发错误,您应该逐步检查该错误以查看原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.