日期:2014-05-20  浏览次数:20678 次

求教.json文件如何在客户端处理
例如我有如下的一个logon.html文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>logon</title>
</head>

<body>
<form id="form1" name="form1" method="POST" action="http://aazj.cn:8080/minisns/logon.json" enctype="application/x-www-form-urlencoded" target="_self">
<table width="500" border="0" cellspacing="0" cellpadding="0" style="border:1px solid #CC3300">
  <tr>
    <td>&nbsp;</td>
    <td><p>logon</p></td>
  </tr>
    <tr>
    <td width="110"><div align="right">mobile(String)</div></td>
    <td width="401"><label>
      <input type="text" name="mobile" id="el_mobile" value="" />
    </label></td>
  </tr>
    <tr>
    <td width="110"><div align="right">password(String)</div></td>
    <td width="401"><label>
      <input type="text" name="password" id="el_password" value="" />
    </label></td>
  </tr>
    <tr>
    <td width="110"><div align="right">code(String)</div></td>
    <td width="401"><label>
      <input type="text" name="code" id="el_code" value="" />
    </label></td>
  </tr>
    <tr>
    <td><div align="right"></div></td>
    <td><label>
      <input type="submit" id="button" value="提交" />
    </label></td>
  </tr>
</table>
</body>
</html>

注意表单中action的url, 我输入用户名和密码, 点击提交之后,服务器发给我一个.json文件该如何解析呢?
是用ajax技术么?

------解决方案--------------------
http://aazj.cn:8080/minisns/logon.json你提交后的Action是怎么写的,
你的返回类型肯定设置成了json,所以才会给你一个.json的文件。
整理下思路。你是想点击提交按钮后跳转页面还是,只是做登入验证。
如果跳转页面,那么你的返回类型换掉。
如果是验证,那么可以按照这种方式提交,只是应该异步Ajax提交,而不是把当前form提交掉
------解决方案--------------------
json文件还是json形式的字符串?
json文件的话是普通的csv文件可读么?
解析json串的话详见:
http://bbs.csdn.net/topics/390337723
------解决方案--------------------
你那个表单中action的url,只是请求的后缀而已,是否返回json还是其他格式的数据要看你后台的配置。然后返回的应该是json数据,而不是.json吧?
------解决方案--------------------
json 格式的数据 可以键值获取
------解决方案--------------------
var success= {"rs":"s","body":{"ukey":"6C99FBB0E098C9F89EA53DEA748ACEA8","token":"3xTecsAYFeXOiOqy","userId":"116"},"pl":6}
alert(success.rs+"\n"+success.body.ukey+"\n"+success.body.token+"\n"+success.body.userId+"\n"+success.pl);
var error={"rs":"f","error":"illegal_argument"}
alert(error.rs+"\n"+error.error);

------解决方案--------------------
引用:
楼上可不可以指示更加详细一点,因为我提交表单之后,服务端直接就把json格式的字符串发送过来了,我怎样

才能将其值赋给一个javascript的变量呢?



示例一:
<script type="text/javascript" >
   var myAjax = new Ajax.Request(
        "http://www.XXX",
        {
            method:"post",       //表单提交方式 
            parameters:"name=acai&age=26&sex=male",   //提交的表单数据
            setRequestHeader:{"If-Modified-Since":"0"},     //禁止读取缓存数据
            onComplete:function(x){    //提交成功回调 
                    alert(x.responseText);
            },
            onError:function(x){          //提交失败回调
                    alert(x.statusText);