Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

php - 将JSON从PHP返回到JavaScript?(Returning JSON from PHP to JavaScript?)

I have a PHP script that's being called through jQuery AJAX.(我有一个PHP脚本,通过jQuery AJAX调用。)

I want the PHP script to return the data in JSON format to the javascript.(我希望PHP脚本将JSON格式的数据返回给javascript。) Here's the pseudo code in the PHP script:(这是PHP脚本中的伪代码:)
$json = "{";
foreach($result as $addr)
{
    foreach($addr as $line)
    {
        $json .= $line . "
";
    }
    $json .= "

";
}
$json .= "}";

Basically, I need the results of the two for loops to be inserted in $json.(基本上,我需要在$ json中插入两个for循环的结果。)

  ask by AquinasTub translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Php has an inbuilt JSON Serialising function.(Php具有内置的JSON Serialising功能。)

json_encode

json_encode(json_encode)

Please use that if you can and don't suffer Not Invented Here syndrome.(如果你可以并且没有遭受Not Invented Here综合症, 使用它。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...