4

I'm trying to pass a dynamic, user created object through AJAX to some C#. I'm not really experienced with JSON, but it seemed like a good method. I'm not sure why, but it's giving me an error on my declaration of the object. (Supposedly.) What am I doing wrong? Thanks.

EDIT: It seems to only error in IE and 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, 28 Mar 2012 14:15:19 UTC

Message: Expected identifier, string 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 Behind

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; }
}
1
  • 1
    Declaration of what object? Post a more detailed description of your error and where you need help. Commented Mar 28, 2012 at 14:12

3 Answers 3

2

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

var json_obj = "{'" + $('#t1').val() + "' : '" + $('#p1').val() + "', '" + $('#t2').val() + "' : '" + $('#p2').val() + "'}";
Sign up to request clarification or add additional context in comments.

2 Comments

That's a string representation of JSON. It's not a JSON object,
or... he could just wrap the json object to JSON.stringify(json_obj). it's a lot safer
0

You could add a function in your C# code such as:

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

Then adjust your JQuery ajax to point at Test() and then in your success function you can do:

msg.Success and msg.CustomJSONAttribute

Comments

0

For what it's worth, I have been struggling with this for hours. I eventually solved my missing parameter issue by ensuring that the JSON object/var in my $.ajax call matched the parameter name in C#. I honestly couldn't believe that this was the issue.

    [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);
                    }
                });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.