关于ajax和php页面接收并返回值的有关问题!

WBOY
Release: 2016-06-13 12:53:55
Original
1036 people have browsed it

关于ajax和php页面接收并返回值的问题!求救!!!急
这是1.php页面

<br />
<script type="text/javascript" src="jquery-1.3.2.min.js"></script><br />
<script type="text/javascript"><br />
$(document).ready(function(){<br />
	$("button").click(function(){<br />
		$.ajax({<br />
	      type: "GET",<br />
	      url: "2.php",<br />
	      data: {id:"123456"},<br />
	      dataType: "json",<br />
	       success: function(msg)<br />
	       {  <br />
		      $("#returncontent").html(msg);<br />
		   } <br />
	});<br />
	});<br />
});<br />
</script><br />
<div id="returncontent">这是要显示的地方</div><br />
<button type="button">按钮</button><br />
Copy after login

这是2.php页面
echo "1111";
?>
这样是可以正常显示,但如果我把1.php$("#returncontent").html(msg);
改成$("#returncontent").html(msg.message);
那么我在2.php应该如何来写才能返回给1.php,我试过用echo "{\"message\":".$_GET['id']."}";的返回id值,但如果我想写成echo "{\"message\":"
";却老是没有显示!!麻烦求教,新手!!!


------解决方案--------------------
echo "{\"message\":"
";
有语法错误。
你的交互方式采用json,即可以用 json_encode() 来输出JSON格式文本
<?php<br />
$data = array(<br />
    'message' => "<div><input type='submit' name='submit' value='提交'></div>"<br />
);<br />
echo json_encode($data);
Copy after login

------解决方案--------------------
$result = array(
    'content' => '具体的内容',
    'message' => 'XX成功',
    'statu' => 1,
);

==========
JS我一般这样
$.post('2.php', {id:123456}, function(data){
    if(data.message)alert(data.message);
    if(data.statu==0)return;
    $('#XXXXX').html(data.content);
    //...更多处理操作a
});
------解决方案--------------------
1、在 IE 系列的浏览器中测试时,要注意清除缓存
2、2.php中的所有可能的输出都要放在一个数组内
输出时用 
echo json_encode($array);

注意:由于 json_encode 只支持 utf-8 编码的中文,所以输出中有中文时要注意转码

------解决方案--------------------


<script></script>
<script><br /> $(document).ready(function(){<br /> $("button").click(function(){<br /> $.ajax({<br /> type: "GET",<br /> <span style="color: #FF0000;"> url: "2.php?id=1234",<br /> success: function(msg)<br /> { <br /> $("#returncontent").html(msg.message);<br /> $("#returncontent1").html(msg.message1);<br /> $("#returncontent2").html(msg.message2);<br /> } <br /> });<br /> });<br /> });<br /> </script>

这是message要显示的地方

这是message1要显示的地方
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!