最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return JSON object from php script - Stack Overflow

programmeradmin1浏览0评论

I am making an AJAX GET request using jQuery to a PHP file. I want the PHP script to return a JSON object, however, currently it is returning a JSON string. I realise I can use JSON.parse in the jQuery code, however, any experience I have in making an AJAX call to an API a JSON object is returned. I am trying to do the same with the php script however, it is returning a string as opposed to an object.

Does anyone know what the best practice is here, and if the best practise is to return a JSON object how I would do this using PHP?

Please see the code below:

js

$.get('test.php', function(data){
    console.log((data));
});

php

<?php

$jsonAnswer = array('test' => 'true');
echo json_encode($jsonAnswer);

I am making an AJAX GET request using jQuery to a PHP file. I want the PHP script to return a JSON object, however, currently it is returning a JSON string. I realise I can use JSON.parse in the jQuery code, however, any experience I have in making an AJAX call to an API a JSON object is returned. I am trying to do the same with the php script however, it is returning a string as opposed to an object.

Does anyone know what the best practice is here, and if the best practise is to return a JSON object how I would do this using PHP?

Please see the code below:

js

$.get('test.php', function(data){
    console.log((data));
});

php

<?php

$jsonAnswer = array('test' => 'true');
echo json_encode($jsonAnswer);
Share Improve this question edited Jul 7, 2016 at 11:00 asked Jul 7, 2016 at 10:56 user6002037user6002037 8
  • What's the problem with JSON.parse? There's probably some jQuery method that parses the response automatically, which might be what you're thinking about. – gcampbell Commented Jul 7, 2016 at 10:58
  • its not that there is a problem with JSON.parse but i just want to know if i can do this server side so that the correct format is received in the browser. does this make sense? – user6002037 Commented Jul 7, 2016 at 10:59
  • You can only send strings over the internet. – gcampbell Commented Jul 7, 2016 at 11:00
  • 1 There's no such thing as a "JSON object" – Andreas Commented Jul 7, 2016 at 11:00
  • 1 That's because jQuery calls JSON.parse for you. Anyway, it works now. – gcampbell Commented Jul 7, 2016 at 11:13
 |  Show 3 more comments

3 Answers 3

Reset to default 13

In your PHP file, change the content type to application/json.

JS

$.get('/process.php', function(data) {      
    console.log(data);
} );

PHP

<?php

    header( "Content-type: application/json" );

    $jsonAnswer = array('test' => 'true');
    echo json_encode($jsonAnswer);

Then your console should read Object {test: "true"} rather than just the JSON string.

Add json to the end of your get function to return json

$.get('test.php', function(data){
    console.log((data));
},'json');//here

and/or add this header in php

header('Content-Type: application/json');

more info here

Without modifying PHP script you can do:

$.get( "test.php", function( data ) {
 var arr = $.parseJSON(data);
 console.log(arr);
 alert(arr.test);
});
发布评论

评论列表(0)

  1. 暂无评论