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"]
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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);
}
});
});
});
Method 2
This is how I did 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 modified
data: "data=" + response.data + "&data1=anyothervaluelikethis", contentType: "application/x-www-form-urlencoded",
Method 3
The codebehind C# method signature should look something like:
[WebInvoke(UriTemplate = "MyMethod", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
public Object MyMethod(Object data){
// your code
}
where Object can be any serializable class
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0