Trying to pass a JSON object to C # using AJAX and jQuery

I am trying to pass a dynamic, user created object via AJAX to some C #. I am not very good at JSON, but it was a good method. I'm not sure why, but this gives me an error in my object declaration. (Presumably.) What am I doing wrong? Thanks.

EDIT: It seems that there is only an error in IE, but I need it to work in IE7.

Webpage Error Details

User agent: Mozilla / 4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; NET4.0C; .NET4.0E; MDDC; InfoPath. 2) Timestamp: Wed, Mar 28, 2012 2:15:19 PM UTC

Message: expected identifier, line or number Line: 18 Char: 21 Code: 0 URI: http: // localhost: 56560 / Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <script type="text/javascript"> $(function() { $('input[type=button').click(function(){ var json_obj = { $('#t1').val() : $('#p1').val(), $('#t2').val() : $('#p2').val()}; $.ajax({ typeof: "POST", url: '/test.aspx', contentType: 'application/json; charset=utf-8', data: json_obj, dataType: 'json', success: function(msg) { alert('Success!'); }, error: function(msg) { alert('Error!'); } }); }); }); </script> </head> <body> <div> Type: 1: <input type="text" id="t1" /> Property 1: <input type="text" id="p1" /> Type 2: <input type="text" id="t2" /> Property 2: <input type="text" id="p2" /> <input type="button" value="Add object!" /> </div> </body> </html> 

Code for

 public class Test { public Test(string json) { JObject jObj = JObject.Parse(json); JToken jUser = jObj["json_obj"]; first = (string)jObj["t1"]; second = (string)jObj["t2"]; } public string first { get; set; } public string second { get; set; } } 
+4
source share
3 answers

I think the format of your json data is wrong. Try the following:

 var json_obj = "{'" + $('#t1').val() + "' : '" + $('#p1').val() + "', '" + $('#t2').val() + "' : '" + $('#p2').val() + "'}"; 
+2
source

You can add a function to your C # code, for example:

  [HttpPost] public JsonResult Test() { return Json(new {Success = true, CustomJSONAttribute="Whatever You Like"}); } 

Then configure jQuery ajax to point to Test (), and then in your success function you can do:

msg.Success and msg.CustomJSONAttribute

0
source

For what it's worth, I struggled with this watch. I eventually solved the problem with the missing parameters, making sure that the JSON / var object in my $ .ajax call matches the parameter name in C #. I honestly could not believe that this was a problem.

  [WebMethod] public static void SetSession(String **json**) { String s = json; } 

...

 var json_obj = "{'" + '**json**' + "' : '" + 'the_val' + "'}"; $.ajax({ type: "POST", url: "my_page.aspx/SetSession", data: json_obj, contentType: "application/json; charset=utf-8", dataType: 'json', success: function () { alert('SetSession executed.'); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Status: " + textStatus); alert("Error: " + XMLHttpRequest.responseText); } }); 
0
source

Source: https://habr.com/ru/post/1403916/


All Articles