消息关闭
    暂无新消息!
php后台怎么实现直接接受前台的JSON对象


<!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 type="text/javascript" src="http://tianjinshuxie.net/frame/jquery/jquery-3.2.1.min.js"></script>
</head>
 
<body>
 
<script type="text/javascript">
var information ={};
information["url0"] = "a12";
information["url1"] = "b32";
information["url2"] = "c52";
var i = 0;
function transformAr(){
 
     
    if(i<3)
    {
        var sa = "url"+i;
        searchMember(information["sa"]);
        i++;
        setTimeout(transformAr(),2000)
    }
     
 
     
}
 
 
function searchMember(abc){
    //alert("检测");
    information["aa"] = abc;
    $.ajax({
        url:'ab.php',
        type:'post',
        dataType:'json',
        data:information,
        success:function(data){
    
        }
    });
}
 
</script>
 
 
 
</body>
</html>


我希望后台可以直接接受这个information,但是用$["POST"]无效,请问用什么办法好?

4个回答

︿ 2
data:information,
改为
data:JSON.stringify(information),

php 获取后
$data = $_POST['data'];
$data = json_decode($data, true);
print_r($data);
︿ 2
information这里要弄成json数据,然后在后台那里用$_POST['information']来接收数据就可以了,。
︿ 1

<?php
$url0 = '';
$url1 = '';
$url2 = '';
$aa = '';

if(isset($_POST['aa'])){
$url0 = $_POST['url0'] . rand(10, 99);
$url1 = $_POST['url1'] . rand(10, 99);
$url2 = $_POST['url2'] . rand(10, 99);

$json_arr = array("url0"=>$url0,"url1"=>$url1,"url2"=>$url2);
$json_obj = json_encode($json_arr);
echo $json_obj;
exit;
}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title> new document </title>
<meta name="keywords" content=""/>
<meta name="description" content=""/>
<script type="text/javascript" src="jquery.min.js"></script>
</head>

<body>
<div id="kkk" class="">

</div>
<script type="text/javascript">
var information ={};
information["url0"] = "a12";
information["url1"] = "b32";
information["url2"] = "c52";

var i = 0;
function transformAr(){
if(i<3)
    {
        var sa = "url"+i;
        searchMember(information[sa]);
        i++;
        setTimeout(transformAr(),2000)
    }
}

function searchMember(abc){
    //alert("检测");
    information["aa"] = abc;
    $.ajax({
        url:'ab.php',
        type:'post',
        dataType:'json',
        data:information,
cache: false, timeout: 100000, async: true,
        success:function(data){
     $('<p>'+data.url0+'<br/>'+data.url1+'<br/>'+data.url2+'</p >').appendTo($("#kkk"));
        }
    });
}
transformAr();
</script>
</body>
</html>
︿ 0

<?php

$json = [];

if(isset($_POST['aa'])){
foreach ($_POST as $key=>$value){
if(strpos($key,'url')==0){
$json[] = $value;
}
}

$json_arr = array("url0"=>$json[0],"url1"=>$json[1],"url2"=>$json[2]);
$json_obj = json_encode($json_arr);
echo $json_obj;
exit;
}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title> new document </title>
<meta name="keywords" content=""/>
<meta name="description" content=""/>
<script type="text/javascript" src="jquery.min.js"></script>
</head>

<body>
<div id="kkk" class="">

</div>
<script type="text/javascript">
var information ={};
information["url0"] = "a12";
information["url1"] = "b32";
information["url2"] = "c52";

var i = 0;
function transformAr(){
if(i<3)
    {
        var sa = "url"+i;
        searchMember(information[sa]);
        i++;
        setTimeout(transformAr(),2000)
    }
}

function searchMember(abc){
    //alert("检测");
    information["aa"] = abc;
    $.ajax({
        url:'ab2.php',
        type:'post',
        dataType:'json',
        data:information,
cache: false, timeout: 100000, async: true,
        success:function(data){
     $('<p>'+data.url0+'<br/>'+data.url1+'<br/>'+data.url2+'</p >').appendTo($("#kkk"));
        }
    });
}
transformAr();
</script>
</body>
</html>