Javascript SyntaxError:JSON中位于位置1的意外标记
我正在制作一个插件。我试图简单地更新Wordpress中的post meta,方法是将数组/对象/json数组发布到以下php代码中:Javascript SyntaxError:JSON中位于位置1的意外标记,javascript,php,jquery,json,post,Javascript,Php,Jquery,Json,Post,我正在制作一个插件。我试图简单地更新Wordpress中的post meta,方法是将数组/对象/json数组发布到以下php代码中: public static function save_settings_fields(){ $myArray = json_decode($_POST['to_save']); wp_send_json(var_dump($_POST['to_save'])); //i can't even get to this point.
public static function save_settings_fields(){
$myArray = json_decode($_POST['to_save']);
wp_send_json(var_dump($_POST['to_save']));
//i can't even get to this point.
$saved_content = json_decode($_POST['to_save']);
$post_id = $saved_content['post_id'];
$this_count = (int)$saved_content['index'];
$foobar = new POB_Save_Settings;
$isdone = $foobar->update_static_content($post_id, $this_count, $saved_content);
if ( is_wp_error( $isdone ) ) {
wp_send_json_error( $isdone );
} else {
wp_send_json( $isdone );
}
}
我的剧本是:
var SaveOptionSettings = function(postid, count){
var save_array = new Array();
save_array[0]= {};
save_array[0]['post_id'] = postid;
save_array[0]['content_count'] = count;
save_array[1] = {};
for(var i=0; i<$('#settings input').length; i++){
save_array[1][$('#settings input').eq(i).attr('id')] = $('#settings input').eq(i).val();
}
for(var i=0; i<$('#settings textarea').length; i++){
save_array[1][$('#settings textarea').eq(i).attr('id')] = $('#settings textarea').eq(i).val();
}
for(var i=0; i<$('#settings select').length; i++){
save_array[1][$('#settings select').eq(i).attr('id')] = $('#settings select').eq(i).val();
}
console.log(save_array, JSON.stringify(save_array));
var pva_ajax_url = pva_params.pva_ajax_url;
$.ajax({
type: 'POST',
url: pva_ajax_url,
dataType: 'json',
data: {
action: 'save_settings_fields',
'to_save': JSON.stringify(save_array)
}, success: function (result) {
M.toast({html: 'Saved!'})
},
error: function(xhr,textStatus,err)
{
console.log("readyState: " + xhr.readyState);
console.log("responseText: "+ xhr.responseText);
console.log("status: " + xhr.status);
console.log("text status: " + textStatus);
console.log("error: " + err);
}
});
}
我所尝试的:
- 对发布的信息使用
json\u解码
- 向AJAX添加
contentType:“application/json”
- 删除字符串化、数据类型、将数据类型更改为文本的所有组合
- 使每个变量获得
。将
ed推入数组
- 使数组成为对象,反之亦然
我做错什么了吗?您的服务器响应以字符串(202)开头, 这不是有效的json
string(202) "[{\"post_id\":15404,\"content_count\":1},{\"label\":\"Options <span>(please select one)</span>\",\"use_conditionals\":\"on\",\"classes\":\"form-buttons\",\"style_id\":\"options\",\"subtitle\":\"test\"}]"
null
这是字符串
你的回答应该是这样的
[{
"post_id": 15404,
"content_count": 1
}, {
"label": "Options <span>(please select one)</span>",
"use_conditionals": "on",
"classes": "form-buttons",
"style_id": "options",
"subtitle": "test"
}]
此行需要删除或添加注释
谢谢@lawrence cherone
此外,如果您想调试内容,使用echo
或print\r
可能会取得更好的效果
使用其中一个选项将您输入的内容打印到页面上,然后在AJAX
success:function(result){…}
中以字符串(202)作为result
返回,
这不是有效的json
string(202) "[{\"post_id\":15404,\"content_count\":1},{\"label\":\"Options <span>(please select one)</span>\",\"use_conditionals\":\"on\",\"classes\":\"form-buttons\",\"style_id\":\"options\",\"subtitle\":\"test\"}]"
null
这是字符串
你的回答应该是这样的
[{
"post_id": 15404,
"content_count": 1
}, {
"label": "Options <span>(please select one)</span>",
"use_conditionals": "on",
"classes": "form-buttons",
"style_id": "options",
"subtitle": "test"
}]
此行需要删除或添加注释
谢谢@lawrence cherone
此外,如果您想调试内容,使用echo
或print\r
可能会取得更好的效果
使用其中一个将您输入的内容打印到页面上,然后在AJAX
success:function(result){…}
中以result
的形式返回,因此基本上删除wp\u send\u json(var\u dump($\u POST['to\u save'])代码>?看起来像这样,但我回答解释JS端的错误它是什么意思//wp_send_json(var_dump($_POST['to_save']);这一行需要注释,我认为他在代码运行之前发送Json 2时删除了所有内容。不,这一切都很好+1只是指出,如果您查看OPs php代码,您会看到调试遗留在其中,这将导致问题。@MackenzieFritschle。。。啊。好。人力资源强积金。任凭其他正在破坏数据的函数摆布;)所以基本上删除wp_send_json(var_dump($_POST['to_save'])代码>?看起来像这样,但我回答解释JS端的错误它是什么意思//wp_send_json(var_dump($_POST['to_save']);这一行需要注释,我认为他在代码运行之前发送Json 2时删除了所有内容。不,这一切都很好+1只是指出,如果您查看OPs php代码,您会看到调试遗留在其中,这将导致问题。@MackenzieFritschle。。。啊。好。人力资源强积金。任凭其他正在破坏数据的函数摆布;)
var_dump()
wp_send_json(var_dump($_POST['to_save']));