您好,欢迎来到酷升汽车网。
搜索
您的当前位置:首页在js中怎么把json当做参数使用

在js中怎么把json当做参数使用

来源:酷升汽车网


这次给大家带来在js中怎么把json当做参数使用,js中把json当做参数使用的注意事项有哪些,下面就是实战案例,一起来看一下。

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

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

JS里字符串转json方法总结

Json怎么实现序列化与反序列化(附代码)

Copyright © 2019- kushenhuo.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务