Home > Backend Development > PHP Tutorial > die(json_encode(. 没有返回

die(json_encode(. 没有返回

WBOY
Release: 2016-06-13 12:07:56
Original
1314 people have browsed it

die(json_encode(... 没返回
我的ajax代码

<br />$.post('{:U("Safeinfo/txpassadd")}', $("#txadd").serialize(), function(data) {<br /> alert("ok");	...<br />
Copy after login


php代码:
<br />tip("100", '<font color="red">交易密码不能与登录密码一样!</font>');<br />
Copy after login


<br />function tip($code, $msg) {<br />    $arr['code'] = iconv('GB2312', 'UTF-8', $code);<br />    $arr['msg'] = iconv('GB2312', 'UTF-8', $msg);<br />	<br />    //die(json_encode($arr));<br />    die(var_json_encode($arr));  //cjq<br />}<br /><br />function var_json_encode($var){ <br /> $_var = var_urlencode($var); <br /> $_str = json_encode($_var); <br /> return urldecode($_str); <br />}<br />
Copy after login


结果页面没有弹出那个alert("ok"),说明ajax没返回?

改成这样也不行:
<br />function tip($code, $msg) {<br />    $arr['code'] = iconv('GB2312', 'UTF-8', $code);<br />    $arr['msg'] = iconv('GB2312', 'UTF-8', $msg);<br />	<br />    die(json_encode($arr,JSON_UNESCAPED_UNICODE));  //cjq<br />}<br />
Copy after login


这是为什么呢?
------解决思路----------------------
safeinfo-txpassadd.html 经 url 重写后实际执行的是
SafeinfoAction::txpassadd 方法
其中用到 tip 函数,不知你是如何定义的

你那 $.post 方法有 json 声明,所以 tip 函数应输出 json 格式串
而 $.post 的回调函数的参数 data 已被解析成 js 对象了
你再 var data1=eval("("+data+")"); 就有蛇足了,应去掉
------解决思路----------------------
你得先把你php代码调试好才行
------解决思路----------------------
tip 函数应写成这样
function tip($code, $msg) {<br />    $arr['code'] = iconv('GB2312', 'UTF-8', $code);<br />    $arr['msg'] = iconv('GB2312', 'UTF-8', $msg);<br />     <br />    die(json_encode($arr));<br />//    die(var_json_encode($arr));  //cjq<br />}<br />
Copy after login

对于 tip("100", '交易密码不能与登录密码一样!');
得 {"code":"100","msg":"\u4ea4\u6613\u5bc6\u7801\u4e0d\u80fd\u4e0e\u767b\u5f55\u5bc6\u7801\u4e00\u6837\uff01<\/font>"}
如果写成
function tip($code, $msg) {<br />    $arr['code'] = iconv('GB2312', 'UTF-8', $code);<br />    $arr['msg'] = iconv('GB2312', 'UTF-8', $msg);<br />     <br />//    die(json_encode($arr));<br />    die(var_json_encode($arr));  //cjq<br />}<br />
Copy after login

则 tip("100", '交易密码不能与登录密码一样!');
得 {"code":"100","msg":""red">交易密码不能与登录密码一样!"}
就错了!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template