做个记号,ajax 从服务器端获取 json 对象

 

ajax 中,通过 req 获取 json, 发送方法如下

  1. var handlerFunction = attrsHandler(req,fAlias);
  2.    req.onreadystatechange = handlerFunction;
  3.   req.open("POST""url"true);
  4.    // Specify that the body of the request contains form data
  5.    req.setRequestHeader("Content-Type""application/x-www-form-urlencoded");   
  6.    // Send form encoded data stating that I want to add the
  7.    // specified item to the cart.
  8.    req.send("a=" + 1 + "&b=-1"  + "&c=" + c);

得到响应代码后,需要做如下处理才可当做 object
        

  1.  var jsonExpression = "(" + req.responseText + ")";
  2.          var family = eval(jsonExpression);

你可能感兴趣的:(做个记号,ajax 从服务器端获取 json 对象)