Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax method call

I am trying to call a simple method in my code behind using Jquery with Ajax. But I get a 404 not found exception everytime. Unfortunately this is a web forms solution. So I dont have all the perks of MVC :(

It does get into the javascript method and gives the alert but won't go into my c# method. My previous experience of using this Jquery method is in an MVC website. Is it compatible with webforms sites?

My Javascript is:

$(document).ready(function() {

              $('#btn_<%=UserStuff.tag %>').click(function() {                    

                  var value = $('#<%#Eval("tag") %>twink').val();
                  something(value);                    
              });
          });


          function something(theval) {

            alert(theval);

              $.ajax({
                  type: "POST",
                  url: "/Default.aspx/MyMethod?something=" + theval,
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function(msg) {
                      alert(msg);
                  }
              });
          }
}

And my C# code is:

   public JsonResult MyMethod(string something)
{
    JsonResult ret = new JsonResult();      

    return ret;
}

Thanks in advance.

like image 583
Funky Avatar asked Dec 22 '10 10:12

Funky


People also ask

Can I call function from AJAX?

The $. ajax() Function. The url parameter is a string containing the URL you want to reach with the Ajax call, while settings is an object literal containing the configuration for the Ajax request. In its first form, this function performs an Ajax request using the url parameter and the options specified in settings .

What does AJAX () method do?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

What is type in AJAX call?

The ajax() method in jQuery is used to perform an AJAX request or asynchronous HTTP request. Syntax: $.ajax({name:value, name:value, ... }) Parameters: The list of possible values are given below: type: It is used to specify the type of request.


1 Answers

Your method returns JsonResult. This is MVC specific and you cannot use it in a webforms application.

If you want to call methods in the code behind in a classic WebForms application you could use PageMethods:

[WebMethod]
public static string GetDate()
{
    return DateTime.Now.ToString();
}

And then to call the method:

$.ajax({
    type: 'POST',
    url: 'PageName.aspx/GetDate',
    data: '{ }',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {
        // Do something interesting here.
    }
});

And here's a full working example I wrote for you:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>
<script type="text/C#" runat="server">
    [WebMethod]
    public static string SayHello(string name)
    {
        return "Hello " + name;
    }
</script>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="/scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: 'POST',
                url: 'default.aspx/sayhello',
                data: JSON.stringify({ name: 'John' }),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {
                    // Notice that msg.d is used to retrieve the result object
                    alert(msg.d);
                }
            });
        });
    </script>
</head>
<body>
    <form id="Form1" runat="server">

    </form>
</body>
</html>

PageMethods are not limited to simple argument types. You could use any type as input and output, it will be automatically JSON serialized.

like image 73
Darin Dimitrov Avatar answered Oct 10 '22 23:10

Darin Dimitrov