JQuery Ajax Post to C #

I am trying to get a JSON object in C #, here is my JavasSciprt post, but I can't pass it to 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 tried to get data like:

 Request.Form["data"] Request["data"] 
+7
source share
3 answers

Here is an example from Encosia.com (I added a form parameter). You do not need to access Page.Form - you can use the 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 content with the page method return. $("#Result").text(msg.d); } }); }); }); 
+12
source

Here's how I did it, and it worked for me:

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

These two lines are changed.

  data: "data=" + response.data + "&data1=anyothervaluelikethis", contentType: "application/x-www-form-urlencoded", 
+2
source

The C # codebehind code signature should look something like this:

 [WebInvoke(UriTemplate = "MyMethod", Method = "POST", ResponseFormat = WebMessageFormat.Json)] public Object MyMethod(Object data){ // your code } 

where Object can be any serializable class

+2
source

All Articles