Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Ajax Post to C#

I'm trying to retrieve JSON Object on C# here is my JavasSciprt post but I'm unable to hande it on codebehind, thanks!

$.ajax({
    type: "POST",
    url: "facebook/addfriends.aspx",
    data: { "data": response.data },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        location = '/facebook/login?URL=' + ReturnURL + '&UID=' + response.authResponse.userID + '&TK=' + response.authResponse.accessToken + '';
    }
});

I've tried to retrieve data like:

Request.Form["data"]
Request["data"]
like image 809
Kaner TUNCEL Avatar asked May 18 '12 13:05

Kaner TUNCEL


1 Answers

Here's an example from Encosia.com (I added a form parameter). You don't need to access Page.Form - you can use method parameters instead.

Codebehind

public partial class _Default : Page 
{
  [WebMethod]
  public static string GetDate(string someParameter)
  {
    return DateTime.Now.ToString();
  }
}

Javascript

$(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
  $("#Result").click(function() {
    $.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: {someParameter: "some value"},
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        $("#Result").text(msg.d);
      }
    });
  });
});
like image 118
jrummell Avatar answered Sep 28 '22 02:09

jrummell