Home  >  Article  >  Web Front-end  >  How to use json as parameter in js

How to use json as parameter in js

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 15:27:362360browse

This time I will show you how to use json as a parameter in js. What are the precautions for using json as a parameter in js. The following is a practical case, let's take a look.

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传递多个数据,防止数据冗余,这里类似于配置信息
});
The following is a simple example:

function testJSON(JSON){
 alert(JSON.name);
 alert(JSON.age);
 alert(JSON.id);
}
testJSON({name:"huangbiao",
  "age":23,
  "id":1});
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Summary of string to json method in JS

How to achieve serialization and deserialization of Json (With code)

The above is the detailed content of How to use json as parameter in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn