我想将键值传递到 php 页面。
在php页面,我会通过匹配ajaxcallid
开始读取值。
但它不起作用。
这与我传递的语法/方式有关导致错误。
parse error
invalid json: ajax call id is missing
JavaScript/AJAX:
var person = {
"Address" : "123 Anywhere St.",
"City" : "Springfield",
"PostalCode" : 99999
};
alert(person);
person= JSON.stringify(person);
alert(person);
$.ajax({
url: 'ROOT_URL/admin/ajaxtest.php',
type: "POST",
dataType: 'json',
data: {ajaxcallid: '26', jsarr: person},
timeout: 5000,
success: function(output) {
alert(output.Address);
},
});
PHP:
<?php
if (isset($_REQUEST['ajaxcallid']))
{
if($_REQUEST['ajaxcallid']==26)
{
//example, I want to read value of person.Address, person.City,
//person.PostalCode
//what is the easiest way
$phparr= json_decode($_REQUEST['jsarr']);
//do all other operation
$output= json_encode($phparr);
}
}
else
{
$output= "ajax call id is missing";
}
echo $output;
?>
最佳答案
如果您没有使用 dataType : 'json',您可能需要执行 stripslashes
$.post(window.data.baseUrl, {posts : JSON.stringify(posts)});
在 php 中:
$posts = json_decode(stripslashes($_POST['posts']));
关于php - json 字符串化为 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4989171/