<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#ffpost').click(function(){
var user = $("#username").val();
$.ajax({
type: "post",
cache:false,
dataType:"json",
url: "dd.php",
data: "username =" + user,
success: function(html){
$("#results").append(html.username);
}
});
})
});
</script></head><body>
<div id="results">888</div>
<form >
<p><span>输入姓名:</span><input type="text" name="username" id="username" /></p>
<p><span>输入密码:</span><input type="text" name="password" id="password" /></p>
</form>
<input name="" id="ffpost" type="button" /></body>
</html>这是dd.php文件的内容<?php
echo json_encode($_POST);调试了2天了,都不对。 也不知道哪里出错啦, 请帮我看一下
改为
data: {username : user},
username后面不能有空格,否则接收不到值
你要把data:"username ="+ user; 写成 data:"username="+user; 去掉空格试试?
在传参时保证data为json格式,并返回json格式,否则出错
php页面的json_encode保证参数是个数组传参时可这样
var myData = {};
myData.userName = $("#username").val();
...
data: $.toJSON(myData),//调用时这样调用好看,要不然你得拼凑好格式