在客户端序列化数据 [英] Serialize data on client side

查看:78
本文介绍了在客户端序列化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
我正在使用Ajax从Web服务中获取数据,并尝试使用eval函数对数据进行反序列化.我尝试了以下

Hello Friends,
I''m fetching data from webservice using ajax and trying to deserialise the data using eval function. I have tried following

success: function(response) 
{
    alert("Begin");
    var myObject = eval(''('' + eval(response) + '')'');
    alert("End");
    Bind(response);
}



我收到Begin警报,但End警报没有出现.



I''m getting Begin alert but End alert is not coming.

Something is going wrong with

var myObject = eval(''('' + eval(response) + '')'');

此行出了什么问题.

对解决该问题有帮助吗?

在此先感谢

this line.

Any help to sort out that issue?

Thanks in advance

推荐答案

为了了解问题所在,您还可以将语句分成两行.这可能会提供更多信息.

In order to understand what''s going wrong you can also split the statement in two lines. That may give some more information.

alert("Begin");
var result1 = eval(response);
alert("Step 1");
//or alert(result1);
var myObject = eval(result1);
alert("End");



也许是语法.试试这个:



Perhaps it is the syntax. try this:

var myObject = eval(eval(response));


我不认为您为此使用eval函数,有关此eval函数的更多信息,请参见此链接.
http://www.w3schools.com/jsref/jsref_eval.asp [
I don''t think you use the eval function for this, see this link for more information on the eval function.
http://www.w3schools.com/jsref/jsref_eval.asp[^]

Instead try this approach:

<body>

<script type="text/javascript">
function updateTable() {
  var oP1 = document.getElementById('div1');
  oP1.innerHTML = "<table><tr><td>test</td></tr></table>";
}

</script>

<input type="button" value="Update table"  önclick="updateTable()" />
<div id="div1"></div>

</body>



选择容器对象(ID为div1的div),然后将响应字符串写入innerHTML.您可能需要在输出字符串中添加表标签.



Selecty the container object (div with id div1) and write the response string to innerHTML. You may need to add table tags to your output string.


这是最终解决方法:
aspx.cs file code
Here is final Solution:
aspx.cs file code
private static string ToString(DataTable table)
    {
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        JavaScriptSerializer js = new JavaScriptSerializer();
        if (table != null)
        {
            if (table.Rows.Count > 0)
            {
                foreach (DataRow dr in table.Rows)
                {
                    sb.Append("<table><tbody><tr><td>");
                    sb.Append((dr["jobno"]));
                    sb.Append("</td><td>");
                    sb.Append((dr["commenttext"]));
                    sb.Append("</td></tr></tbody></table>");
                }
            }
        }
        //js.Serialize(sb, sb1);
        return sb.ToString();
    }



aspx file code (JQuery with AJAX Code)



aspx file code (JQuery with AJAX Code)


这篇关于在客户端序列化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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