jquery $.ajax() 应用中的一点困惑,望高手点拨
test.php文件内容
-----------------------------------
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/Javascript">
$(document).ready(function(){
$(".ajax_btn").click(function(){
$.ajax({
type:"POST",
url:"test2.php",
data:"name=12345",
success:function(data){
alert( data);
}
});
})
})
</script>
</head>
<body>
<button class="ajax_btn">clickme</button>
</body>
</html>
---------------
test2.php文件内容
-----------------
<?php
$name=$_POST["name"];
echo $name;
?>
--------------------
调试的内容
=================(Request-Line) POST /php/jobass/test2.php HTTP/1.1
Accept */*
Accept-Encoding gzip, deflate
Accept-Language zh-CN
Cache-Control no-cache
Connection Keep-Alive
Content-Length 0
Content-Type application/x-www-form-urlencoded; charset=UTF-8
DNT 1
Host localhost
Referer http://localhost/php/jobass/test.php
User-Agent Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
X-Requested-With XMLHttpRequest
=================================================
问题在于,我用POST方式时候,弹出的ALERT对话框值为空,而用GET却能正确显示12345。这让我很困惑,求解惑。
------解决方案--------------------
$.ajax({
type:"POST",
url:"test2.php",
data:{
name: 12345
},
success:function(data){
alert( data);
}
});
改成这样
------解决方案--------------------
结贴吧,我看了连接的页。你也可以在php中写出header头,就无需dataType:text
<?php
header('Content-Type: text/html; charset=utf-8');
...