1

我必须在 jquery ajax 中发送位置大小及其父详细信息并通过 PHP 获取它们

我的代码是:-

$("#save").click(function(){ 
        var pos=[];
        $(".dr").each(function(index){
        dragId=$(this).attr("id");
        topPos=$("#"+ dragId).position().top;
        left=$("#"+ dragId).position().left;
        dragLeft=left/10;
        dragLeft=dragLeft ? dragLeft:0;
        dragTop=topPos/10;
        dragTop=dragTop ? dragTop :0;
        dragWidth=$("#"+dragId).width();
        dragHeight=$("#"+dragId).height();
        parentDivWidth=$("#"+dragId).parent().width();
        parentDivheight=$("#"+dragId).parent().height(); 
        parentDivClass=$("#"+dragId).parent().attr("class");
        var obj = {};
        obj = {left: dragLeft,top :dragTop,dragWidth:dragWidth,dragHeight:dragHeight,parentDivWidth:parentDivWidth,parentDivheight:parentDivheight,parentDivClass:parentDivClass}; 
         pos[$(this).attr("id")]=obj;
     })
    $.ajax({
            type: "POST",
             url:"<?php echo Yii::app()->request->baseUrl?>/index.php/BillSettings/savePositions",
             data:{pos:pos},
             dataType:'html',
             success: function(res){
                 console.log(res);  
          }
        })



 });

PHP 代码

var_dump($_REQUEST);

但我无法获得 $_REQUEST 或 $_REQUEST['pos'] 的价值。任何帮助都应不胜感激。

4

3 回答 3

0

js:

$.ajax({
    type: "POST",
    data:{pos: JSON.stringify(pos},
    //...

php:

var pos = json_decode($_REQUEST['pos']);
var_dump(pos);

是你想要的吗?

于 2013-11-01T08:12:04.563 回答
0

尝试将要通过 ajax 传递的对象转换为字符串

$.ajax({
            type: "POST",
             url:"<?php echo Yii::app()->request->baseUrl?>/index.php/BillSettings/savePositions",
             data: JSON.stringify(pos),
             dataType:'html',
             success: function(res){
                 console.log(res);  
          }
        })

然后在 php

$pos = json_decode($_REQUEST['pos']);
于 2013-11-01T08:12:21.020 回答
0

js:

$.ajax({
         type: "POST",
         url:"<?php echo Yii::app()->request->baseUrl?>/index.php/BillSettings/savePositions",
         data:{"pos":pos},
         cache: false,
         success: function(res){
             console.log(res);  
      }
    })

php:

  $post=$_POST["post"];
于 2013-11-03T08:56:16.447 回答