asp.net mvc 2 - Send generic JSON data to MVC2 Controller -
i have javascript client going send json-formatted data set of mvc2 controllers. client format json, , controller have no prior knowledge of how interpret json model. so, can't cast controller method parameter known model type, want grab generic json , pass factory of sort.
my ajax call:
function sendobjectasjsontoserver(object,url,idforresponsehtml) { // make call server process object var jsonifiedobject = json.stringify(object); $.ajax({ url: url // set caller , datatype: 'json' , data: jsonifiedobject , type: 'get' , error: function(data) { alert('error in sendobjectasjsontoserver:' + data); } , success: function(data) { alert(data.message); // note data parsed object } }); } my mvc controller:
public actionresult saveform(string obj) { // ok, try saving object string rc = passjsontosomething(obj.tostring()); string message = "{\"message\":\""+rc+"\",\"foo\":\"bar\"}"; return new contentresult { content = message, contenttype = "application/json" }; } the problem obj null. can tell me how should structure ajax call , controller parameter json server? i'm using mvc2. may appear duplicate of questions, in case not know model json maps to, can't use specific model type in controller parameter type.
thanks much.
have tried that?
$.ajax({ url: url // set caller , datatype: 'json' , data: {obj :jsonifiedobject} , contenttype: 'application/json; charset=utf-8' , type: 'get' , error: function(data) { alert('error in sendobjectasjsontoserver:' + data); } , success: function(data) { alert(data.message); // note data parsed object } });
Comments
Post a Comment