syntax of passing json object using Ajax

sharir4u

New Member
Im trying to pass these json object to the code behind\[code\]var obj = { Name: '1', Starting: '3', Timeline: [ { StartTime: '111', GoesFor: '111' } , { StartTime: '112', GoesFor: '112' } ] };\[/code\]of course the next step is to stringify the object\[code\]var data = http://stackoverflow.com/questions/12719584/JSON.stringify(obj);\[/code\]after that I use a jquery ajax call to pass the value to code behind\[code\]$.ajax({ url: 'default.aspx/test', type: 'POST', contentType: 'application/json', data: data, success: function(result) { $('#result').html(result.d); } });\[/code\]the point is im getting error form the jquery library\[code\]POST http://localhost:63050/default.aspx/test 500 (Internal Server Error)\[/code\]the problem is solved when I remove the obj variable, and put it into the JSON.stringfy method, like this\[code\]var data = http://stackoverflow.com/questions/12719584/JSON.stringify({ obj: { Name:'1', Starting: '3', Timeline: [ { StartTime: '111', GoesFor: '111' } , { StartTime: '112', GoesFor: '112' } ] } });\[/code\]Is there anyway to passing the entire object variable into the json.stringify function, instead of declare and initializing within the function?In case you guys wanted to know, my code behind looks something like this\[code\]public class MyModel{ public string Name { get; set; } public string Starting { get; set; } public testing2[] Timeline { get; set; }}public class testing2{ public string StartTime { get; set; } public string GoesFor { get; set; }}[WebMethod] public static string Test(MyModel obj) { return "Hello from test" + obj.Name + obj.Starting + obj.Timeline[0].StartTime + obj.Timeline[1].StartTime; }\[/code\]
 
Back
Top