php - json 字符串化为 php

标签 php jquery json

我想将键值传递到 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/

相关文章:

javascript - 使用 setTimeout 时 e.nodename 未定义

jquery - 创建上标jqeryui按钮

javascript - 如何使用 Jekyll _data 目录中的 JSON?

javascript - 单击开关时如何在 2 个 div 之间平滑切换?

javascript - 使用 get json 方法从 json 动态获取数据并循环值

javascript - 解析来自 Google Places API 的 JSON 结果

php - 尝试在 slim 中使用 twig 时出错, fatal error :TwigView::getEnvironment()

javascript - 处理哈希数据的最佳算法

php - 为 MYSQL 中的 foreach() 提供的参数无效,适用于第一个表而不是第二个表

PHP 获取超过 20000 封 imap 电子邮件