0

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

5
  • is GET allowed for web service? Commented Aug 27, 2013 at 7:12
  • I think all web methods have to be static, make your web method like this public static string getdata(){ //rest of code} Commented Aug 27, 2013 at 7:14
  • @VishweshwarKapse i have made it static but still some thing is missing because its not working Commented Aug 27, 2013 at 7:19
  • Mitesh, How are you determining if the method is working or not? Your javascript based success code is blank and so is the failure code. Commented Aug 27, 2013 at 8:55
  • @Kami i am inserting some value in my table...and its not inserting due to this code Commented Aug 27, 2013 at 9:13

5 Answers 5

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() 
{ 
Sign up to request clarification or add additional context in comments.

9 Comments

an alert box of hello is only coming
Public static string GetData() should be changed to Public static string getdata()........and i had tried this but this time alert box of hello is also not showing
@MiteshJain I hope you are using GET method rather than POST so you have to allow your method to execute this using GET method .Check the attribute on on GetData function , i have updated it , sorry!!
really its not working .can i have your email? I wanna give u my demo
@MiteshJain Well mitesh can you check your GetData function ? Create a simple function and test it i will give you a function check it please .Create a new page and put above code in it and check . and also put debug point on your GetData function and check if this function is being called or not .
|
0

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

Comments

0

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

3 Comments

Also make your webmethod static so that i can be called from client side
I have tried using writting url:"default.aspx/getdata" and url:"getdata" but not working
I dont no why i had written "success: function (data) { }" i just have copied from searching other type of question...is it necessary?
0

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

2 Comments

what error you are getting, i have given a working example after testing it.
do i have to pass any thing in string value????..if not than it is not showing any thing on the button click
0

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.

2 Comments

What is the error message? "not working" covers allot of ground. Please be more specific. The code is working and testing in my environment.
actual on clicking of button non of the response is getting nor any error is showing. It seems to be that as i have only place a button and non of the code or any of the click event is placed on that button

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.