[英]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.