简体   繁体   English

在jquery Ajax中发布对象

[英]Post objects in jquery Ajax

I have to send position size and their parent details in jquery ajax and get them by PHP 我必须在jquery ajax中发送头寸大小及其父级详细信息,并通过PHP获取它们

My code is :- 我的代码是:-

$("#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 code PHP代码

var_dump($_REQUEST);

But I can not get value of $_REQUEST or $_REQUEST['pos'].Any help should be appreciated. 但是我无法获得$ _REQUEST或$ _REQUEST ['pos']的值。任何帮助都值得感谢。

js: JS:

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

php: PHP:

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

Is it what you want? 是你想要的吗?

try converting the object you want to pass via ajax to a string 尝试将您要通过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);  
          }
        })

then in php 然后在PHP

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

js: 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: PHP:

  $post=$_POST["post"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM