如何格式化Ajax使用的序列化数据 [英] How do I format serialized data to be used by Ajax

查看:187
本文介绍了如何格式化Ajax使用的序列化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将数据序列化为此格式

I have serialized data into this form"

string json1 = [{"id":1,"col1":"11","col2":"22","col3":"33"}]

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "javascript", "Javascript:GetDateTime(" + json1 + ");", true);



如何更改json1的格式以便我不会收到此错误:


How do I change the format of json1 so I do not get this error:

"Error Code: Status: [object Object] Ex: error"



jQuery Ajax


jQuery Ajax

    <script  type="text/javascript src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
        function GetDateTime(json1) {
            $.ajax
            ({
                type: "POST",
                url: "Default.aspx/GetServerDateTime",
                data: json1,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    alert(result.d);
                },
                error: function (status, ex) {
                    alert("Error Code: Status: " + status + " Ex: " + ex);
                }
            });
        }
    </script>

推荐答案

.ajax
({
类型: POST
url: Default.aspx / GetServerDateTime
data:json1,
contentType: application / json; charset = utf-8
dataType: json
成功:功能(结果){
alert(结果) .d);
},
错误: function (status,ex){
alert( 错误代码:状态: + status + 例如: + ex);
}
});
}
< / script>
.ajax ({ type: "POST", url: "Default.aspx/GetServerDateTime", data: json1, contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { alert(result.d); }, error: function (status, ex) { alert("Error Code: Status: " + status + " Ex: " + ex); } }); } </script>


不要这样做。



始终序列化 - ASP.NET中的JSON序列化和反序列化 [ ^ ]。
Don't do like this.

Always Serialize - JSON Serialization and Deserialization in ASP.NET[^].


这篇关于如何格式化Ajax使用的序列化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆