PHP前端$.ajax传递数据到后台

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="jquery-2.1.0.min.js"  type="text/javascript"></script>
    <script>
    $(function(){
        $("#btn").click(function(){
                var my_data="前台变量";
                my_data=escape(my_data)+"";//编码,防止汉字乱码
                $.ajax({
                    url: "index.php",  
                    type: "POST",
                    data:{trans_data:my_data},
                    error: function(){  
                        alert('Error loading XML document');  
                    },                   
                    complete:function()
                    {
                        location.href = "index.php?trans_data=" + my_data;
                    }/* ,  
                    success: function(data,status){//如果调用php成功    
                        alert(unescape(data));//解码,显示汉字
                    } */
                });
        });            
    });     
    </script>
  </head>
  <body>
    <form action="index.php" method="post">
      <input type="text" name="title" value="标题"/>
      <input  type="submit" value="提交"/>
      <input id="btn" type="button" value="点击"/>
    </form>
  </body>
</html>

php代码

 <?php 
     header('Content-Type: text/html; charset=gb2312');  
     $backValue=$_GET['trans_data'];
     echo $backValue;
  ?>

  

posted @ 2016-10-29 21:42  爱编程1314  阅读(13046)  评论(0编辑  收藏  举报