>  기사  >  php教程  >  Jquery和PHP Ajax JSON

Jquery和PHP Ajax JSON

WBOY
WBOY원래의
2016-06-06 19:54:26971검색

无缓存,无错版 $.ajax({ type: GET, url: index.php, cache: false, data: con=Addact=_searchkey= + key+id=+id, dataType:json, success: function(msg){ bindGroupList(msg); } }); //绑定 function bindGroupList(result) { var eles = document.forms[

无缓存,无错版
$.ajax({
   type: "GET",
   url: "index.php",
   cache: false,
   data: "con=Add&act=_search&key=" + key+"&id="+id,
   dataType:"json",
   success: function(msg){
   bindGroupList(msg);
   }
});

//绑定
function bindGroupList(result)
{
   var eles = document.forms['theForm'].elements;
   eles['group_id'].length = 1;
   for (i = 0; i    {
     var opt = document.createElement('OPTION');
     opt.value = result.content[i].id;
     opt.text  = result.content[i].name;
     eles['group_id'].options.add(opt);
   }
}

会缓存
$.getJSON("?con=Add&act=_search&key=" + key+"&id="+id, function(data){
    //bindGroupList(data)
});

=====php make_json_result=======
/**
 * 创建一个JSON格式的数据
 *
 * @access  public
 * @param   string      $content
 * @param   integer     $error
 * @param   string      $message
 * @param   array       $append
 * @return  void
 */
function make_json_response($content = '', $error = "0", $message = '', $append = array()) {
 $res = array(
  'error' => $error,
  'message' => $message,
  'content' => $content
 );
 if (!empty($append)) {
  foreach ($append AS $key => $val) {
   $res[$key] = $val;
  }
 }
 $val = json_encode($res);

 exit($val);
}

/**
 *
 *
 * @access  public
 * @param
 * @return  void
 */
function make_json_result($content, $message = '', $append = array()) {
 make_json_response($content, 0, $message, $append);
}

 

return  make_json_result($list);

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.