简体   繁体   中英

calling function in the javascript written in webpage(.cs)

I have function on .cs page

[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;
}

My Html page code is

<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>

I am trying to call my web side function to client side on submit click. Am I missing something? Please give a demo from my above code

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

On your url try : "PageName.aspx/MethodName". Also check out this blog post by Dave Ward :

Using jQuery to directly call ASP.NET AJAX page methods

Below line is error prone. do not include "()" in url method name.

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

Replace above line with

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

See the following working example

// Code behind method declared static

[WebMethod]
public static string GetSquare(String value)
{
    return "hello" + value;
}

your button whose click this has to be done

<input type="button" id="button" value="Chnageurl" onclick="ajaxcall()" />

script for this

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

From your comments I have gathered that you are verifying if this method is working by checking for new entries in a database table. The data in the database could be missing for other reasons rather than the query. To verify, try a more simple web method, and go from there.

eg,

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!";
    }

If you do not see a success response, then the problem is indeed with your javascript, or rather with the site setup which is somehow preventing callbacks from javascript.

If the method succeeds then it is likely that your database insertion script is raising an error and you should step through it to see that cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM