-2

I'm using a mysql query on my database and building a row array using fetch_array() function in my php script and converted it into json. Now in my ajax success code in can see the row in my console log as bellow

"{"0":"84","id":"84","1":"btv","news_name":"btv","2":"BTV","news_title":"BTV","3":"Bangladesh Televesion","news_description":"Bangladesh Televesion","4":"/management/template/img/default_logo.png","news_logo":"/management/template/img/default_logo.png","5":"Dhaka, Bangladesh","news_address":"Dhaka, Bangladesh","6":"free","subs_status":"free","7":"33","newscreatedbyID":"33","8":"2014-08-21","newscreateddate":"2014-08-21","9":null,"sponsored":null,"10":"0","protectstatus":"0","11":"0","approved":"0"}"

My js success function is as follow:

$.ajax({
url: "/function/news_user_page.php",
type: "POST",
data: {news_name:'84'},
success:function(data, textStatus, jqXHR)
        {
         $("#test").html(data.news_title);
         console.log(data);

        },
error: function(jqXHR, textStatus, errorThrown)
        {
            //if fails     
        }

})

I can see the whole data in my consol log but in my html element named test the news_title is now showing. the data.news_title is not working. How to access my converted json data? i am bit confused.

  • Reason may be it is not properly converted to json. [Look Here](http://stackoverflow.com/a/7986636/1823242) – Vasu Adari Aug 22 '14 at 19:55

3 Answers3

3

Look like you have a stringy version, set the dataType in your AJAX request to automatically convert it to JSON:

dataType: "JSON",

Or, use JSON.parse on the response

var parsed = JSON.parse(data);
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • 1
    In order to specify desired response format, isn't it `dataType: "json",` (lowercase) ? [http://api.jquery.com/jquery.ajax/#data-types](http://api.jquery.com/jquery.ajax/#data-types) – cssyphus Aug 22 '14 at 19:53
  • 2
    @gibberish -- Dont think casing matters in this case. Seen it both ways. – tymeJV Aug 22 '14 at 19:54
  • @tymeJV I've one more confusion. In my php script i'm echoing the json data. is there any way to send the database to my ajax calling only? my php code is as follows: i want it will send the json data to my ajax call when only any ajax called the data –  Aug 22 '14 at 19:58
  • `query($querynews); $rownews=$resultnews->fetch_array(); echo json_encode($rownews); } newsdetails($inquired_news); ?>` –  Aug 22 '14 at 20:00
  • 1
    @nafizaalam -- Not sure, PHP isn't my strong suit - post another question. – tymeJV Aug 22 '14 at 20:06
2

Try setting the dataType to json

0

Echoing your data will just return a string. Use echo json_encode( $data ); instead.

Here is what you can do in PHP to ensure json is sent from your server:

<?PHP
$data = /** whatever you're serializing --- array/object **/;
header('Content-Type: application/json');
echo json_encode($data);

Returning JSON from a PHP Script

Community
  • 1
  • 1
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • Is there any other way to response in ajax call without use **"echo"** in php? –  Aug 22 '14 at 20:06