ajax post数据提交 PHP后台接收不到数据

c860819222 2015-04-09 02:55:41
JS的脚本提交
$.ajax({
url:"/music/json.php",
type:"POST",
data:{"id":id},
dataType:"json",
success: function(e){




PHP后台:

//这里不知道怎么接收数据
echo $_POST["id"]; //不行
echo json_decode($_POST['data']);//不行
//下面就是数据库查询
$query="select * from gequ where id='$_POST[id]'";//执行查询语句不正确,尝试各种方法如上面获取ID的数据未果来求助
$result=mysql_query( $query );



传值如下:
id:xxxx
看了论坛上有关帖子,始终不能解决问题,在不改变JS的脚本的情况下,如何在后台接收传值
...全文
13273 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
1_bit 2017-07-11
  • 打赏
  • 举报
回复
引用 13 楼 df981011512 的回复:
你和在我之前回帖的人都有点搞笑!ajax操作能直接打印吗?这种一般情况只能抓包看数据或者写文件调试!别连这点常识都没有好吧?
。。。google 浏览器可以在network查看
  • 打赏
  • 举报
回复
你和在我之前回帖的人都有点搞笑!ajax操作能直接打印吗?这种一般情况只能抓包看数据或者写文件调试!别连这点常识都没有好吧?
baidu_36633895 2016-11-07
  • 打赏
  • 举报
回复
楼主解决没啊?我后台接收到的ajax数据只有个空数组啊。。。
U_DO1024 2016-11-07
  • 打赏
  • 举报
回复
k'h'j'k'h'k
qw7843251 2015-08-27
  • 打赏
  • 举报
回复
楼主能不能讲讲怎么改的。。。我也想不到其他办法了。。
zhangsheng_1992 2015-04-09
  • 打赏
  • 举报
回复
我说的datatype不用写是如果不牵扯到跨域传值或者js的数组 /对象 的话 字符串完全可解决 所以就没必要加datatype 比如你要已json格式传递 另外 url:"/music/", 很简单就能理解 就是当前目录下的/music/ 比如你访问www.xxx.com/music/ 那么如果设置了默认文件了 会直接访问默认文件的 比如默认文件时index.php那么实际就是www.xxx.com/music/index.php 另外json_decode()有2个参数 第一个是json 第二个如果不想要对象的话请写true
c860819222 2015-04-09
  • 打赏
  • 举报
回复
引用 5 楼 willflyhigh 的回复:

<html>
    <head>
        <title>title</title>
        <script src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
        <script>
         $(function(){
            $("#btn").click(function(){
                alert('ok!');
                var id = 3;
                $.ajax({
                    url:"2.php",
                    method:"POST",
                    dataType:"json",
                    data:{id:id},
                    success:function(data){
                       alert(data.id);
                    }
                    
                    
                });
                
            });
        });
        </script>
    </head>
    <body>
        <input type="button" id="btn" value="button">
    </body>
</html>

2.php文件

<?php
$dd['id'] = $_POST['id'];
$dd['value'] = 'fff';
echo json_encode($dd);
?>
另外楼上有位说 dataType不用写,下面是引用官方文档的解释。
引用
dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. I
引用 7 楼 willflyhigh 的回复:
我擦 楼主你看看5楼, 你看看 官方给的 介绍:http://api.jquery.com/jquery.ajax/
引用
dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:
你理解错我意思了,dataType是要写的,我问题主要是json_decode而已,你是对的
willflyhigh 2015-04-09
  • 打赏
  • 举报
回复
我擦 楼主你看看5楼, 你看看 官方给的 介绍:http://api.jquery.com/jquery.ajax/
引用
dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:
c860819222 2015-04-09
  • 打赏
  • 举报
回复
引用 1 楼 zhangsheng_1992 的回复:
正常提交请不要加dataType:"json", 否则你需要print_r(json_decode($_POST['id']))这个是数组 不能echo
一下就懂了,缺少json_decode啊,赞个,分就给你了 顺便再问个跟这个没多大关系的问题 $.ajax({ url:"/music/", type:"POST", data:{"id":id}, dataType:"json", success: function(e){ 如果URL是这样传递的,这是什么技术呢
willflyhigh 2015-04-09
  • 打赏
  • 举报
回复

<html>
    <head>
        <title>title</title>
        <script src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
        <script>
         $(function(){
            $("#btn").click(function(){
                alert('ok!');
                var id = 3;
                $.ajax({
                    url:"2.php",
                    method:"POST",
                    dataType:"json",
                    data:{id:id},
                    success:function(data){
                       alert(data.id);
                    }
                    
                    
                });
                
            });
        });
        </script>
    </head>
    <body>
        <input type="button" id="btn" value="button">
    </body>
</html>

2.php文件

<?php
$dd['id'] = $_POST['id'];
$dd['value'] = 'fff';
echo json_encode($dd);
?>
另外楼上有位说 dataType不用写,下面是引用官方文档的解释。
引用
dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. I
ChanKalam 2015-04-09
  • 打赏
  • 举报
回复
data:{"id":id}, 在JS里面alert一下这个id,看是否有值
zhangsheng_1992 2015-04-09
  • 打赏
  • 举报
回复
另外
$query="select * from gequ where id='$_POST[id]'";
如果你post过来的id是int类型的话 完全没必要加单引号
zhangsheng_1992 2015-04-09
  • 打赏
  • 举报
回复
刚写了ajax post删除 给你发出来吧

 function deleteall(){
                var checkboxs = document.getElementsByName("selects");
                var ids = new Array();
                 for(var i = 0 ; i<checkboxs.length ; i++){
                    if(checkboxs[i].checked == true){
                        ids[i] = checkboxs[i].value;
                    }
                }
                if(ids.length<1){
                    return false;
                }
                var newids = ids.join(",");
                if(confirm("确认删除?")){
                    $.ajax({
                     type: "POST",
                     url: "<?php echo url('autoReply','deleteall')?>",
                     data:"id="+newids,
                     success: function(e){
                        if(e == 1 ){
                           for(var i = 0 ; i<checkboxs.length ; i++){
                            if(checkboxs[i].checked == true){
                                checkboxs[i].checked = false;
                            }
                            } 
                          location.reload(); 
                      }else{
                          alert("删除失败");
                      }
                     }
                    })
                }
            }
zhangsheng_1992 2015-04-09
  • 打赏
  • 举报
回复
正常提交请不要加dataType:"json", 否则你需要print_r(json_decode($_POST['id']))这个是数组 不能echo

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧