在网页(.cs)中编写的javascript中调用函数

时间:2013-08-27 07:10:16

标签: c# javascript asp.net

我在.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>

我试图在提交点击时将我的网络侧功能调用到客户端。 我错过了什么吗?请从上面的代码中提供演示

5 个答案:

答案 0 :(得分:1)

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() 
{ 

答案 1 :(得分:0)

在您的网址上尝试:&#34; PageName.aspx / MethodName&#34;。另请参阅Dave Ward撰写的这篇博客文章:

Using jQuery to directly call ASP.NET AJAX page methods

答案 2 :(得分:0)

以下行容易出错。不要在url方法名称中包含“()”。

$.ajax({ type: "GET", url: "/getdata()", success: function (data) { });

替换上面的行
$.ajax({ type: "GET", url: "/getdata", success: function (data) { });

答案 3 :(得分:0)

请参阅以下工作示例

// 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"); }
 });
};

答案 4 :(得分:0)

根据您的评论我收集到您通过检查数据库表中的新条目来验证此方法是否正常工作。由于其他原因而不是查询,数据库中的数据可能会丢失。要验证,请尝试更简单的Web方法,然后从那里开始。

例如

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的回调。

如果方法成功,那么您的数据库插入脚本可能会引发错误,您应该单步执行它以查看原因。