从AJAX请求中获取PHP中的JSON数组

时间:2021-05-24 12:15:54

I'm sending an AJAX request with multiple arrays and I can't figure out how to grab the data?

我正在发送一个带有多个数组的AJAX请求,我不知道如何获取数据?

This is what I'm sending:

这就是我要发送的:

从AJAX请求中获取PHP中的JSON数组

I'm doing this through a jQuery AJAX POST to a PHP file.. How would one go about grabbing the data from here?

我通过jQuery AJAX发布到PHP文件来实现这一点。如何从这里获取数据呢?

Thank you!

谢谢你!

-- EDIT!!

——编辑! !

This is the jQuery

这是jQuery

var h1 = []
h2 = []
h3 = [],
layout = $( "input[type=radio][name='layout_option']:checked" ).val();

$("ul.widget-order[name='1'] li").each(function() {  h1.push($(this).attr('id'));  });
$("ul.widget-order[name='2'] li").each(function() {  h2.push($(this).attr('id'));  });
$("ul.widget-order[name='3'] li").each(function() {  h3.push($(this).attr('id'));  });

var sendData = JSON.stringify({
    ids1: " " + h1 + "",
    ids2: " " + h2 + "",
    ids3: " " + h3 + ""
});

$.ajax({
    type: "POST",
    url: "_backend/account/updateWidgets.php",
    data: { data: sendData } + '&layout=' + layout,
    success: function( data ){
        $("#post_reply").html(data);
        console.log( { data: sendData } );
    )};
)};

5 个解决方案

#1


0  

Assuming that the post request to your Php file is successful you can access it by using the $_POST variable.

假设对Php文件的post请求成功,您可以使用$_POST变量访问它。

#2


1  

To read the JSON encoded data on the PHP side:

读取PHP端的JSON编码数据:

<?php

$json = file_get_contents('php://input');
$decodedJSON = json_decode($json);

?>

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.json-decode.php

#3


1  

$_POST['data'] contains JSON, so call json_decode().

$_POST['data']包含JSON,因此调用json_decode()。

$data = json_decode($_POST['data'], true);

Now you can access $data['ids1'], $data['ids2'], etc.

现在您可以访问$data['ids1']、$data['ids2']等。

There isn't really any good reason to send the data as JSON. You could just put the original object directly in the jQuery data: option. Then you would could access the parameters as $_POST['ids1'], $_POST['ids2'], etc.

没有任何好的理由将数据作为JSON发送。您可以直接将原始对象放在jQuery数据:选项中。然后可以访问参数为$_POST['ids1']、$_POST['ids2']等。

$.ajax({
  type: "POST",
  url: "_backend/account/updateWidgets.php",
  data: {
    ids1: " " + h1,
    ids2: " " + h2,
    ids3: " " + h3,
    layout: layout
  },
  success: function(data) {
    $("#post_reply").html(data);
    console.log({
      data: sendData
    });
  }
});

#4


0  

If I may, I would recommend changing the way you are making the jQuery request to the following:

如果可以的话,我建议您将jQuery请求的方式改为:

var h1 = []
h2 = []
h3 = [],
layout = $( "input[type=radio][name='layout_option']:checked" ).val();

$("ul.widget-order[name='1'] li").each(function() {  h1.push($(this).attr('id'));  });
$("ul.widget-order[name='2'] li").each(function() {  h2.push($(this).attr('id'));  });
$("ul.widget-order[name='3'] li").each(function() {  h3.push($(this).attr('id'));  });

var sendData = JSON.stringify({
    ids1: " " + h1 + "",
    ids2: " " + h2 + "",
    ids3: " " + h3 + ""
});
var sPayload = "data=" + encodeURIComponent(sendData) + "&layout=" + encodeURIComponent(layout);
$.ajax({
    type: "POST",
    url: "_backend/account/updateWidgets.php",
    data: sPayload,
    success: function( data ){
        $("#post_reply").html(data);
        console.log( { data: sendData } );
    }
});

There were a few syntax errors in code you posted which I have corrected in the code included in my answer, but most importantly, I have modified your data to being all of one encoding. It seems like you were trying to combine a JSON Object with standard data encoded as application/x-www-form-urlencoded. I have standardized this in the code above so all data is posted as application/x-www-form-urlencoded.

您发布的代码中有一些语法错误,我已经在我的回答中包含的代码中进行了更正,但最重要的是,我将您的数据修改为所有的一个编码。看起来您试图将JSON对象与作为application/ www-form- urlencodes编码的标准数据相结合。我已经在上面的代码中对它进行了标准化,所以所有的数据都被发布为应用程序/x-www-form-urlencode。

In your PHP, you can then access this as:

在PHP中,您可以如下方式访问:

<?php
// First, let's get the layout because that's the easiest
$layout = (isset($_POST['layout']) ? $_POST['layout'] : NULL);
// As a note, the isset(...) with ternary operator is just my preferred method for checking missing data when extracting that data from the PHP super-globals.

// Now we'll need to get the JSON portion.
$jData = $_POST['data'];
$data = json_decode($jData, $assoc=true);

echo "ids1 is " . $data['ids1'] . "\n<br>\n";
echo "ids2 is " . $data['ids2'] . "\n<br>\n";
echo "ids3 is " . $data['ids3'] . "\n<br>\n";
?>

#5


0  

Follow this code

遵循这个代码

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

I think have a mistake between data is property of ajax object and data is property in your post data.

我认为在数据是ajax对象的属性和数据是post数据的属性之间有一个错误。

Please post your jquery code.

请发布jquery代码。

#1


0  

Assuming that the post request to your Php file is successful you can access it by using the $_POST variable.

假设对Php文件的post请求成功,您可以使用$_POST变量访问它。

#2


1  

To read the JSON encoded data on the PHP side:

读取PHP端的JSON编码数据:

<?php

$json = file_get_contents('php://input');
$decodedJSON = json_decode($json);

?>

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.json-decode.php

#3


1  

$_POST['data'] contains JSON, so call json_decode().

$_POST['data']包含JSON,因此调用json_decode()。

$data = json_decode($_POST['data'], true);

Now you can access $data['ids1'], $data['ids2'], etc.

现在您可以访问$data['ids1']、$data['ids2']等。

There isn't really any good reason to send the data as JSON. You could just put the original object directly in the jQuery data: option. Then you would could access the parameters as $_POST['ids1'], $_POST['ids2'], etc.

没有任何好的理由将数据作为JSON发送。您可以直接将原始对象放在jQuery数据:选项中。然后可以访问参数为$_POST['ids1']、$_POST['ids2']等。

$.ajax({
  type: "POST",
  url: "_backend/account/updateWidgets.php",
  data: {
    ids1: " " + h1,
    ids2: " " + h2,
    ids3: " " + h3,
    layout: layout
  },
  success: function(data) {
    $("#post_reply").html(data);
    console.log({
      data: sendData
    });
  }
});

#4


0  

If I may, I would recommend changing the way you are making the jQuery request to the following:

如果可以的话,我建议您将jQuery请求的方式改为:

var h1 = []
h2 = []
h3 = [],
layout = $( "input[type=radio][name='layout_option']:checked" ).val();

$("ul.widget-order[name='1'] li").each(function() {  h1.push($(this).attr('id'));  });
$("ul.widget-order[name='2'] li").each(function() {  h2.push($(this).attr('id'));  });
$("ul.widget-order[name='3'] li").each(function() {  h3.push($(this).attr('id'));  });

var sendData = JSON.stringify({
    ids1: " " + h1 + "",
    ids2: " " + h2 + "",
    ids3: " " + h3 + ""
});
var sPayload = "data=" + encodeURIComponent(sendData) + "&layout=" + encodeURIComponent(layout);
$.ajax({
    type: "POST",
    url: "_backend/account/updateWidgets.php",
    data: sPayload,
    success: function( data ){
        $("#post_reply").html(data);
        console.log( { data: sendData } );
    }
});

There were a few syntax errors in code you posted which I have corrected in the code included in my answer, but most importantly, I have modified your data to being all of one encoding. It seems like you were trying to combine a JSON Object with standard data encoded as application/x-www-form-urlencoded. I have standardized this in the code above so all data is posted as application/x-www-form-urlencoded.

您发布的代码中有一些语法错误,我已经在我的回答中包含的代码中进行了更正,但最重要的是,我将您的数据修改为所有的一个编码。看起来您试图将JSON对象与作为application/ www-form- urlencodes编码的标准数据相结合。我已经在上面的代码中对它进行了标准化,所以所有的数据都被发布为应用程序/x-www-form-urlencode。

In your PHP, you can then access this as:

在PHP中,您可以如下方式访问:

<?php
// First, let's get the layout because that's the easiest
$layout = (isset($_POST['layout']) ? $_POST['layout'] : NULL);
// As a note, the isset(...) with ternary operator is just my preferred method for checking missing data when extracting that data from the PHP super-globals.

// Now we'll need to get the JSON portion.
$jData = $_POST['data'];
$data = json_decode($jData, $assoc=true);

echo "ids1 is " . $data['ids1'] . "\n<br>\n";
echo "ids2 is " . $data['ids2'] . "\n<br>\n";
echo "ids3 is " . $data['ids3'] . "\n<br>\n";
?>

#5


0  

Follow this code

遵循这个代码

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

I think have a mistake between data is property of ajax object and data is property in your post data.

我认为在数据是ajax对象的属性和数据是post数据的属性之间有一个错误。

Please post your jquery code.

请发布jquery代码。