使用jquery和html5 post json格式数据

创建html页面引入jQuery

client.html:
<!DOCTYPE HTML >
<html>
<head>
   <title>Test</title>
//引入3个jquery文件
    <script type="text/javascript" src="jQuery/jquery-1.7.2.js"></script>
    <script type="text/javascript" src="jQuery/jquery.json.js"></script>
    <script type="text/javascript" src="jQuery/md5.js"></script>

</head>
<body >
<script type="text/javascript">
    $(function() {
         var hash =hex_md5("phoneNumber=15210943874&key=99999999");//md5加密
       var dat='{"PhoneNumber":"15210943874","authKey":"'+hash+'"}';
       var obj=JSON.parse(dat);
//       var dat='{"phoneNumber":"15210943874"}';
       console.log('the post message : ',dat);
       $.ajax({//ajax方式post数据
           url:'http://lydfanfan.bj100.com/index.php?g=Api&m=user&a=registerphone',
           type:'POST',
           data: obj,  //jsonData为此次post提交时,提交的参数,你可自定义其名称
          dataType:"text",
           timeout:10000,
           error:function(){alert('Error posting data');},
           success:function(result){
              console.log('response :',result);
          }//function
       });
    });
</script>
</body>
</html>

json.js:
(function($) {
    functiontoIntegersAtLease(n)
       // Format integers to have at least twodigits.
    {
       return n < 10 ? '0' + n :n;
    }

    Date.prototype.toJSON =function(date)
       // Yes, it polutes the Date namespace, but we'llallow it here, as
       // it's damned usefull.
    {
       return date.getUTCFullYear()  + '-' +
          toIntegersAtLease(date.getUTCMonth() + 1) + '-' +
          toIntegersAtLease(date.getUTCDate());
    };

    var escapeable =/["\\\x00-\x1f\x7f-\x9f]/g;
    var meta = {   // table of charactersubstitutions
       '\b': '\\b',
       '\t': '\\t',
       '\n': '\\n',
       '\f': '\\f',
       '\r': '\\r',
       '"' : '\\"',
       '\\': '\\\\'
    }

    $.quoteString =function(string)
       // Places quotes around a string,inteligently.
       // If the string contains no control characters,no quote characters, and no
       // backslash characters, then we can safely slapsome quotes around it.
       // Otherwise we must also replace the offendingcharacters with safe escape
       // sequences.
    {
       if (escapeable.test(string))
       {
           return '"'+ string.replace(escapeable, function (a)
           {
              var c = meta[a];
              if (typeof c === 'string'){
                 return c;
              }
              c = a.charCodeAt();
              return '\\u00' + Math.floor(c/ 16).toString(16) + (c % 16).toString(16);
           }) +'"'
       }
       return '"' + string + '"';
    }

    $.toJSON = function(o,compact)
    {
       var type = typeof(o);

       if (type == "undefined")
           return"undefined";
       else if (type == "number" || type =="boolean")
           return o +"";
       else if (o === null)
           return"null";

       // Is it a string?
       if (type == "string")
       {
           return$.quoteString(o);
       }

       // Does it have a .toJSON function?
       if (type == "object"&& typeof o.toJSON =="function")
           returno.toJSON(compact);

       // Is it an array?
       if (type != "function"&& typeof(o.length) =="number")
       {
           var ret =[];
           for (var i= 0; i < o.length; i++) {
              ret.push( $.toJSON(o[i],compact) );
           }
           if(compact)
              return "[" + ret.join(",") +"]";
           else
              return "[" + ret.join(", ") +"]";
       }

       // If it's a function, we have to warnsomebody!
       if (type == "function") {
           throw newTypeError("Unable to convert object of type 'function' tojson.");
       }

       // It's probably an object, then.
       ret = [];
       for (var k in o) {
           varname;
           var type =typeof(k);

           if (type== "number")
              name = '"' + k + '"';
           else if(type == "string")
              name =$.quoteString(k);
           else
              continue; //skip non-string or number keys

           val =$.toJSON(o[k], compact);
           if(typeof(val) != "string") {
              // skip non-serializablevalues
              continue;
           }

           if(compact)
              ret.push(name + ":" +val);
           else
              ret.push(name + ": " +val);
       }
       return "{" + ret.join(", ") + "}";
    }

    $.compactJSON =function(o)
    {
       return $.toJSON(o, true);
    }

    $.evalJSON =function(src)
       // Evals JSON that we know to be safe.
    {
       return eval_r("(" + src + ")");
    }

    $.secureEvalJSON =function(src)
       // Evals JSON in a way that is *more*secure.
    {
       var filtered = src;
       filtered = filtered.replace(/\\["\\\/bfnrtu]/g,'@');
       filtered =filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');
       filtered =filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');

       if (/^[\],:{}\s]*$/.test(filtered))
           returneval_r("(" + src + ")");
       else
           throw newSyntaxError("Error parsing JSON, source is not valid.");
    }
})(jQuery);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值