echo json_encode返回json对象,在前端获取到的却是字符串,而非json对象

WBOY
Release: 2016-06-23 14:38:43
Original
2172 people have browsed it

前端使用一下代码提交并处理后续结果

function del(id){ if(confirm('确认要删除此关键字吗?')){ $.post('./keyword',{ op:'del',id:id },function(result){ alert(result); //正常这个部分应该显示的是object:object,但现在显示的是一个字符串 if( result.code != 0 ){ alert("删除失败!"); }else{ location.reload(); } }); }}
Copy after login

服务端使用以下代码返回
echo json_encode(array("code"=>0));exit;
Copy after login


正常情况下在前端alert result这个变量,应该显示的是object:object,可现在显示的却是这个json字符串。


回复讨论(解决方案)

$.post('./keyword',{ op:'del',id:id },function(result){...}, 'json');

function del(id){ if(confirm('确认要删除此关键字吗?')){ $.post('./keyword',{ op:'del',id:id },function(result){ alert(result); //正常这个部分应该显示的是object:object,但现在显示的是一个字符串 if( result.code != 0 ){ alert("删除失败!"); }else{ location.reload(); } },"json"); }}
Copy after login

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!