使用jquery和html5 post json格式数据

来源:互联网 发布:淘宝天猫京东哪个好 编辑:程序博客网 时间:2024/06/10 11:22
创建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>
md5.js下载

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);




原创粉丝点击