【问题标题】:Trying to pass a JSON object to C# using AJAX and JQuery尝试使用 AJAX 和 JQuery 将 JSON 对象传递给 C#
【发布时间】:2012-03-28 14:10:08
【问题描述】:

我正在尝试通过 AJAX 将用户创建的动态对象传递给某些 C#。我对 JSON 并没有真正的经验,但它似乎是一个好方法。我不知道为什么,但它在我声明对象时给了我一个错误。 (据说。)我做错了什么?谢谢。

编辑:它似乎只在 IE 中出错,但我需要它在 IE7 中工作。

网页错误详情

用户代理:Mozilla/4.0(兼容;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) 时间戳:2012 年 3 月 28 日星期三 14:15:19 UTC

消息:预期的标识符、字符串或数字 线路:18 字符:21 代码:0 网址: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>

代码背后

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

【问题讨论】:

  • 声明什么对象?发布更详细的错误描述以及您需要帮助的地方。

标签: c# jquery ajax json


【解决方案1】:

我认为您的 json 数据格式错误。试试这个:

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

【讨论】:

  • 这是 JSON 的字符串表示形式。它不是 JSON object,
  • 或者......他可以将 json 对象包装到 JSON.stringify(json_obj)。它更安全
【解决方案2】:

您可以在 C# 代码中添加一个函数,例如:

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

然后调整你的 JQuery ajax 指向 Test() 然后在你的成功函数中你可以这样做:

msg.Success 和 msg.CustomJSONAttribute

【讨论】:

    【解决方案3】:

    对于它的价值,我已经为此苦苦挣扎了好几个小时。我最终通过确保我的 $.ajax 调用中的 JSON 对象/var 与 C# 中的参数名称匹配来解决了我缺少参数的问题。老实说,我不敢相信这是问题所在。

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

    【讨论】:

      猜你喜欢
      • 2014-06-12
      • 1970-01-01
      • 2017-03-02
      • 2014-07-28
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多