简体   繁体   中英

JSON response not objects from php

I'm using a php proxy to bypass cross domain ajax issues

My php:

return array(
        'status' => 'ok',
        'header' => $header,
        'content' => $content
    );

So now my $response['content'] holds my content, but i also want my headers.

If i:

echo $response['content'];

Then i get my response like: 在此处输入图片说明

Which is what i need! but i also want to have the $response[header] in the echo. But if i:

echo $response;

I get: 在此处输入图片说明

How can I get both in a json response so i can then use it in my javascript code? And why is it just returning plaintext 'Array'

If i use json_encode its not formatted properly like in my first example.

在此处输入图片说明

Its lost all its formatting..?

You have to parse the respond data $content with json_decode()

$data = array(
    'status' => 'ok',
    'header' => $header,
    'content' => json_decode($content)
);

header('Content-type: application/json');
echo json_encode($data);

Alternatively you can return JSON directly, without parsing

header('Content-type: application/json');
echo '{"status":"ok", "header":"' . $header . '", "content":"' . $content . '"}';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM