function getajaxdata(urlstr, callback_func, options){ var myurl = ajax_header + urlstr + ajax_tail; var isasync = true;//初始化是否同步的属性设置 var ntimeout = ajax_timeout;//初始化请求超时的数据 var errorcallback = null; //利用json对象options来修改默认初始化的属性,这样一个参数可以设置多个属性 if (options) { if (options.sync) //sync这个参数就是json的对象 { isasync = (options.sync === true) ? false : true; } if (options.timeout) { ntimeout = parseint(options.timeout); if (isnan(ntimeout)) ntimeout = ajax_timeout; } errorcallback = options.errorcb; } if ($.browser.mozilla) { try { //netscape.security.privilegemanager.enableprivilege(universalbrowserread); } catch (exception) { log.error(exception); } } $.ajax({ async: isasync, //cache: false, type: get, timeout: ntimeout, url: myurl, //datatype: ($.browser.msie) ? text : xml, error: function(xmlhttprequest, textstatus){ try { if (jquery.isfunction(errorcallback)) { errorcallback(xmlhttprequest, textstatus); } log.error(main : getajaxdata( + myurl + ) error.); log.error(main : xmlhttprequest.readystate = + xmlhttprequest.readystate); log.error(main : xmlhttprequest.status = + xmlhttprequest.status); log.error(main : textstatus + textstatus); } catch (exception) { log.error(exception); } }, success: function(data){ log.debug(main : getajaxdata( + myurl + ) sucess.); log.trace(data); var xml; if (typeof data == string || typeof data == number) { if (!window.activexobject) { var parser = new domparser(); xml = parser.parsefromstring(data, text/xml); } else { //ie xml = new activexobject(microsoft.xmldom); xml.async = false; xml.loadxml(data); } } else { xml = data; } if (typeof callback_func == function) { callback_func($(xml)); } else { log.error(callback_func is undefined or not a function); } } }); } getajaxdata(api/monitoring/status, function($xml){ var wlan_ret = xml2object($xml); if(wlan_ret.type == response) { monitoring_status = wlan_ret.response; setcurrrentuserhtml(); } }, { sync:true //通过json传递多个数据,防止数据冗余,这里类似于配置信息 });
下面是一个简单的例子:
function testjson(json){ alert(json.name); alert(json.age); alert(json.id); } testjson({name:huangbiao, age:23, id:1});
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
js里字符串转json方法总结
json怎么实现序列化与反序列化(附代码)
以上就是在js中怎么把json当做参数使用的详细内容。
