Home  >  Article  >  Backend Development  >  ajax传值跟回调原理

ajax传值跟回调原理

WBOY
WBOYOriginal
2016-06-13 12:04:22804browse

ajax传值和回调原理
刚开始学习ajax,
大牛门帮我看看有没有问题?

<br />$.get("__URL__/yanz",{bid:bid,b:v,},function(data,status){<br />			alert(data.aa);<br />			alert(status);<br />		},"json");<br />


<br />if ($cdata){<br />	 		echo json_encode($cdata);<br />	 		$this->ajaxReturn($cdata,'查询成功',1);//主要是这里回调<br />	 	}else{<br />$this->error("数据不存在");<br />}<br />

用是thinkphp的,本想做一个把ID和名称通过ajax传值,然后查询数据库中是否存在。不存在跳转URL。
问题:值收到了,但回调和跳转URL不知道怎么搞。大牛请指导,

------解决方案--------------------
设置 config
‘DEFAULT_AJAX_RETURN’ => 'JSON'

将返回如下结构
{
  "status“ : "1",
  "info" : "查询成功",
  "data" : 你的$cdata形成的数据体
}

假设你的数据体中有 aa 一项,则 alert(data.data.aa)

注意:不要 echo json_encode($cdata);
切记!因为你要返回的是 json,所以不能有其他任何输出,否则 json 将解码失败

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