json 字符串化为 php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4989171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:27:50  来源:igfitidea点击:

json stringify to php

phpjqueryjson

提问by i need help

I want to pass the key values into php page.

我想将键值传递到 php 页面。

At php page, I will start to read value by matching ajaxcallid.

在 php 页面,我将通过匹配ajaxcallid.

But it not working.

但它不起作用。

It gotta do with syntax/way I am passing in causing error.

这与我传入的语法/方式有关,导致错误。

parse error
invalid json: ajax call id is missing    

JavaScript/AJAX:

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:

<?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;
?>

回答by Evalds Urtans

If you are not using dataType : 'json', you might need to do stripslashes

如果你没有使用 dataType : 'json',你可能需要做 stripslashes

$.post(window.data.baseUrl, {posts : JSON.stringify(posts)});

And in php:

在 php 中:

$posts = json_decode(stripslashes($_POST['posts']));

回答by amol challawar

This helped me:

这帮助了我:

 data = json_decode($this->request->data['jsarr'], true);

in your php code for accessing the record

在用于访问记录的 php 代码中

Hope it will help someone!

希望它会帮助某人!

回答by muddybruin

I'm going to take a guess and say that you SHOULDN'T stringify anything. I believe JQuery will do that for you. Namely, no person = JSON.stringify(person). Give that a try.

我要猜测一下,你不应该把任何东西串起来。我相信 JQuery 会为你做到这一点。即,no person = JSON.stringify(person)。试一试吧。

回答by Peter Oram

This is what your $.ajaxcall and the PHPside should look like:

这就是您的$.ajax呼叫和PHP侧面应该是这样的:

JQuery

查询

$.ajax({
    url: "/admin/ajaxtest.php",
    method: "POST",
    data: {
        ajaxcallid: "26",
        person: JSON.stringify({
            "Address" : "123 Anywhere St.",
            "City" : "Springfield",
            "PostalCode" : "99999"
        })
    }
}).done(function(data) {
    if (!data) {
        // generic error message here
    } else if (data == 'invalid') {
        alert('no ajaxcallid received');
    } else {
        var result = $.parseJSON(data); // if you pass back the object
        alert(result.Address);
    }
});

PHP

PHP

if (isset($_REQUEST['ajaxcallid'])) {
    if ((int) $_REQUEST['ajaxcallid'] == 26) {
        $personData = json_decode($_REQUEST['person']);
        $address = $personData->Address;
        $postalCode = $personData->PostalCode;
        $returnData = json_encode($personData);
        echo $personData;
        die();
    }
} else {
    echo 'invalid';
    die();
}

回答by TheVillageIdiot

I have not worked with PHPbut from my experience with ASP.netfollowing may help you.

我没有工作过,PHP但根据我的经验,ASP.net以下可能会对您有所帮助。

Add contentTypekey to ajax settigns:

contentType键添加到 ajax 设置:

type: "POST",
contentType:'application/json',
dataType: 'json',

also I think you need to stringifywhole value you are assigning to datalike this:

我也认为你需要像这样stringify分配给你的全部价值data

var person = { 
     "Address"    :   "123 Anywhere St.", 
     "City"       :   "Springfield", 
     "PostalCode" :   99999
};

var d= {ajaxcallid: '26', jsarr: person};
var dat=JSON.stringify(d);


......
data: dat,
......