您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2025/12/17 0:59:49发布19次查看
这次给大家带来在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});
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
js里字符串转json方法总结
json怎么实现序列化与反序列化(附代码)
以上就是在js中怎么把json当做参数使用的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product