js在ajax中的数据处理

00、eval转换对象

        if ($.type(data) == 'string')
        {
            data=data.replace(/\s/ig, ' ');
            return eval('(' + data + ')');
        }   

00、序列化表单

        json格式: data:$form.serializeArray();

        字符串方式:$form.serialize;

00、字符串拼接

      1.html=''+
                '<td>表格</td>'+
                '<td id=" '+iterm.value+' ">'+iterm.anwser+'</td>';
        2、事件传参  

<!--<a οnmοuseοver="onMouseOverbox2('an_e91c6f66b00840b4963c0415e3aee274',

                                                                   'ae91c6f66b00840b4963c0415e3aee274_0',this);">

          查看

 </a>-->
        '<a οnmοuseοver="onMouseOverbox2(\' an_ '+questionid+' \',

                                                                      \' a '+questionid+' _ '+index+' \',this);">'+  

          '查看</a>'

00、数据循环

result1=[{"addflag":false,"anwser":"d,d","anwser_result":"0","anwser_time":"2013-03-21","history_id":"6334148648eb40298f67fd16c5ea427a","id":"50a6e441a501435595720377b2d72f69","question_id":"f7ad50d52d48480ca21d3be32d4022a0","scores":0,"seq_no":3,"showAll":true,"std_area_id":"3","std_id":"03010701","std_name":"达芬奇","tch_id":"","tch_name":"","tch_scores":0,"tch_suggestion":"","tch_time":"","uncertain_flag":"0"},      {"addflag":false,"anwser":"1,1","anwser_result":"0","anwser_time":"2013-03-21","history_id":"0cf2a2ae37be46eeb19ad505db42d726","id":"a09223db67b5443cabbe928909610286","question_id":"f7ad50d52d48480ca21d3be32d4022a0","scores":0,"seq_no":2,"showAll":true,"std_area_id":"3","std_id":"03010701","std_name":"达芬奇","tch_id":"","tch_name":"","tch_scores":0,"tch_suggestion":"","tch_time":"","uncertain_flag":"0"}]
result2=[{ "addflag":false,   "anwser":"<p>\r\n\t1<\/p>\r\n","anwser_result":"0",
      "anwser_time":"2013-03-21",   "history_id":"0cf2a2ae37be46eeb19ad505db42d726",
      "id":"a1f03bb16dfa44e09fe9a43b1d8cca4e",    "question_id":"fd4b3956a14244d8a9f10e0fa536792d",
      "scores":0,"seq_no":1,    "showAll":false,    "std_area_id":"3",
      "std_id":"03010701",    "std_name":"达芬奇",    "tch_id":"",
      "tch_name":"",    "tch_scores":0,
      "tch_suggestion":"",    "tch_time":"",
      "uncertain_flag":"0"}]
questionid='Action 私有属性';

 

result=eval('(' + result + ')');
$.each(result,function(index,iterm){
     if(iterm.showAll){
	 html=html+'<tr>'+
              '<td>'+iterm.anwser_time+'</td>'+
              '<td id="a'+questionid+'_'+index+'">'+iterm.anwser+'</td>'
              '</tr>';
     }

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以按照以下步骤实现: 1. 在页面添加一个表单,包含需要显示的数据。 2. 添加一个按钮或链接,用于触发Ajax请求。 3. 使用jQuery或其他JavaScript库编写Ajax请求代码,发送请求到服务器。 4. 在服务器端处理请求,将表单数据JSON格式返回。 5. 在客户端使用Ajax的成功回调函数,将返回的JSON数据解析,并将数据显示在对话框。 以下是一个简单的示例代码,供参考: HTML代码: ```html <form id="myForm"> <label for="name">姓名:</label> <input type="text" id="name" name="name"><br> <label for="age">年龄:</label> <input type="text" id="age" name="age"><br> <label for="gender">性别:</label> <select id="gender" name="gender"> <option value="male">男</option> <option value="female">女</option> </select> </form> <button id="showData">显示数据</button> <div id="dialog"></div> ``` JavaScript代码: ```javascript $(document).ready(function() { $("#showData").click(function() { $.ajax({ url: "getData.php", // 替换为处理请求的服务器端脚本文件 type: "POST", data: $("#myForm").serialize(), // 将表单数据序列化为字符串 dataType: "json", success: function(data) { $("#dialog").html("姓名:" + data.name + "<br>年龄:" + data.age + "<br>性别:" + data.gender); // 将返回的JSON数据解析,并将数据显示在对话框 }, error: function() { alert("请求失败,请稍后重试!"); } }); }); }); ``` 在服务器端脚本文件(例如getData.php),可以使用以下代码将表单数据转换为JSON格式并返回: ```php <?php $name = $_POST['name']; $age = $_POST['age']; $gender = $_POST['gender']; $data = array("name" => $name, "age" => $age, "gender" => $gender); echo json_encode($data); ?> ``` 当用户点击“显示数据”按钮时,客户端将发送Ajax请求到服务器端处理脚本文件(getData.php),并将表单数据作为请求参数发送。服务器端脚本将表单数据转换为JSON格式并返回,客户端使用Ajax的成功回调函数将返回的JSON数据解析,并将数据显示在对话框

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值