[英]Wordpress - Posting Json encoded Array to PHP returns nothing/NULL
我的插件站点上有多个输入。 我收集这些值并将它们推入数组。 然后使用jQuery.post
方法将其发布到我的PHP脚本中。
Javascript:
var json = JSON.stringify(output); // output is an array with multiple strings
var data = {
"action" : "add",
"output" : json,
}
jQuery.post(ajaxurl, data, function(response){
console.log(response); // logs the response from PHP
})
PHP:
$array = json_decode($_POST['output']);
update_option("option", $array);
var_dump($array); // returns NULL in console
echo $array; // returns nothing in console
wp_die();
我希望PHP将数组返回到JS并将其保存为选项。
这是我的第一个问题。 请随时给我有关如何改善我的问题和代码的提示。
发布的JSON字符串中的“ \\”似乎有问题。
$array = json_decode(stripslashes($_POST['output']))
完成了工作
如果您希望脚本返回与该对象相同的对象:
// The second param "true" makes sure we get an associative array
// instead of an object.
$array = json_decode($_POST['output'], true);
update_option("option", $array);
// Set the correct content header (good practice)
header('Content-Type: application/json');
// Echo the string representation of the json-object
// Let's use the same as we got in the $_POST['output']
echo $_POST['output'];
wp_die();
找到了解决方案。 它写在“解决方案”下的问题中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.