[asp.net] json 데이터를 mvc controller 로 보낼때
Sending json to asp.net mvc controller does make objects, but doesn't fill properties
http://stackoverflow.com/questions/20307983/sending-json-to-asp-net-mvc-controller-does-make-objects-but-doesnt-fill-prope
So I want to post my jsondata that I've received from the facebook javascript SDK on my asp.NET webserver. The problem that I'm getting is that the facebookfriend-objects are being created (facebookfriendlist is populated), but aren't being filled (id = null, name = null for each object in the facebookfriendlist).
Json format
{"data":[{"name":"friend x","id":"integer here"},{"name":"friend y","id":"integer here"},...],"paging":{"next":"tokenXXX"}}
My jquery post (json is filled, I've printed the content into a div to check)
FB.api('/me/friends', function (response) {
$("#friends").html(JSON.stringify(response));
pb.progressbar("value", 66);
$.ajax({
url: '/MapMe/Retrieve',
type: 'POST',
data: response,
accept: 'application/json',
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
pb.progressbar("value", 100);
//window.location.href = './App';
}
function errorFunc() {
alert("Failed to get Data")
}
});
My controller:
[HttpPost]
public ActionResult Retrieve(FacebookFriendlist friends)
{
System.Diagnostics.Debug.WriteLine(friends);
return Json(new {status = true});
}
My objectclasses
public class FacebookFriendlist
{
public FacebookFriendlist()
{
data = new List<Facebookfriend>();
}
public IList<Facebookfriend> data { get; set; }
public string paging { get; set; }
}
public class Facebookfriend
{
public string name { get; set; }
public string id { get; set; }
}
For starters, you have accept
in the jQuery .ajax()
function, but it should be accepts
, like this:
$.ajax({
url: '/MapMe/Retrieve',
type: 'POST',
data: response,
accepts: 'application/json',
success: successFunc,
error: errorFunc
});
Per jQuery .ajax()
documentation:
accepts (default: depends on DataType)
The content type sent in the request header that tells the server what kind of response it will accept in return. If the accepts setting needs modification, it is recommended to do so once in the $.ajaxSetup() method.
'Web > ASP.NET MVC' 카테고리의 다른 글
[asp.net] MVC 에서 viewModel과 model 매핑 및 편집 방법 (2) | 2015.04.27 |
---|---|
Introducing ASP.NET MVC 3 (Preview 1) (2) | 2015.04.27 |
[Asp.net] MVC Controller 에서 jQuery로 동적으로 데이터 얻는 방법 (2) | 2015.04.27 |
[Asp.net] Json 을 MVC controller 로 Posting 하는 방법 (2) | 2015.04.27 |
[ASP.net] Json 문자열을 컨트롤로로 보내고 deserialize 하는 방법 (2) | 2015.04.27 |