<html> <head> <script language="JavaScript" type="text/javascript" src="js/jquery.js"></script> <script language="JavaScript" type="text/javascript" src="js/jquery.timer.js"></script> <script type="text/JavaScript"> $(document).ready(function(){ $('#submitbt').click(function(){ //var name = $('#name').val(); //var dataString = "name="+name; var dataPass = { 'name': $("#name").val() }; $.ajax({ type: "POST",url: "post.PHP",//data: dataString,字符串传值方式 data: dataPass,//json传值方式 success: function (data) { alert(data); var re = $.parseJSON(data || "null"); console.log(re); } }); }); }); </script> </head> <body> <form id="form" name="form" id="myform" method="post"> <label>Name</label> <input name="name" type="text" id="name" /> <input type="button" class="button" id="submitbt" value="submit" > </form> </body> </html>
<?PHP $name = $_POST['name']; echo json_encode(array('name'=>$name));//为什么return就不行呢? ?>
参考:检测json格式: http://jsonlint.com/