Home > Backend Development > PHP Tutorial > php用ajax为何只能返回数字

php用ajax为何只能返回数字

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:48:19
Original
836 people have browsed it

php用ajax为什么只能返回数字

<br />//提交表单<br />function subm(){<br />		var user = $('#frm').serialize();<br />		$.ajax({<br />			url: 'b.php',<br />			type: 'POST',<br />			data:user,<br />			contentType:"application/json",<br />			dataType: 'json',<br />			async:false,<br />			beforeSend: function(){<br />				alert('Error loading PHP document');<br />			},<br />			success: function(result){<br />				alert(result);<br />			}<br />		});<br />	}<br />
Copy after login

<br /><form id="frm" action="" method="post" onsubmit="subm()"><br /><input<br />	id="user" name="username" /><br /><br /><input id="password" type="password" name="password" /><br /><br /><br /><input type="submit" value="提交" /></form>
Copy after login



然后我在b.php页面写一个 echo '123' 回调函数可以执行alert(res) 并输出123 可是我echo写字母字符中文他就没反应了 连框都不弹  我把页面设成utf-8 在 b.php也加了header 可是他应该能返回字母跟字符 就算乱码也会弹个窗吧 是不是那个json传递的问题 求教
------解决方案--------------------
你有 dataType: 'json' 
表示回传的应该是一个 json 串

显然非数字串因为缺少引号而导致解析失败
------解决方案--------------------
你的dataType是json形式,在php中就要返回json形式的数据
可以用json_encode(),转下返回

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