PHP - 在 PHP 中检索 ajax 发布的 json 对象

标签 php jquery ajax json

我正在使用此函数将 json 发送到 php 页面:

function update_records(data) {
    data = data;
    $.ajax({
        type: 'POST',
        cache: false,
        timeout: 2000,
        contentType: 'application/json',
        url: 'update.php',
        data: data, //'data='+data+'&aid=0',
        success: function() {
            success_message('success');
        },
        error: function(){
            failure_message('failure');
        }
    }); 

在 firebug 中我可以看到发布的数据:

[{
    "postid": 66,
    "values": [
        "field_key=a",
        "oldvalue=b",
        "newvalue=c dad"
    ]
}]

在我的 php 页面上如何$_REQUEST 对象?还是我做错了?

最佳答案

我实际上确实在我的 js 上使用此代码解决了这个问题

这就是我收集数据的方式 我已经创建了对象

jsonObj={"postid":postid,"value":value};
var jsonString=JSON.stringify(jsonObj);

现在我使用ajax发送对象

$.ajax({
  type: 'POST',
  url: siteurl+'/wp-content/themes/crm/modules/update_lead.php',
  dataType : 'json',
  data: {action:actionType,data:data},
});

这位于目标 PHP 文件 (/wp-content/themes/crm/modules/update_lead.php)

$json=json_decode(stripslashes($_POST['data']), true);

希望这有帮助......

关于PHP - 在 PHP 中检索 ajax 发布的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9326566/

相关文章:

php - 如何获取逗号分隔字符串的子字符串?

php - Symfony3 : Login error : Authentication request could not be processed

javascript - 刷新 jquery 中的 ui-Dialog 弹出窗口

javascript - 如何在Windows8移动应用程序中禁用触摸覆盖

javascript - Jquery Colorbox - 保留加载动画,直到单独的 ajax 内容准备好

java - 没有一个适用于 PHP 的 MVC 框架具有完整的功能,如持久性 api、ORM、注释和依赖注入(inject)

php - 当授权 key 是我的服务器 key 时,Firebase MismatchSenderID

jquery 问题与 html 高度 :100%

javascript - 通过Ajax动态更新数据列表

javascript - Joomla Ajax jQuery