-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.

1
  • Reason may be it is not properly converted to json. Look Here Commented Aug 22, 2014 at 19:55

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

5 Comments

In order to specify desired response format, isn't it dataType: "json", (lowercase) ? http://api.jquery.com/jquery.ajax/#data-types
@gibberish -- Dont think casing matters in this case. Seen it both ways.
@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
<?php require_once("../configuration.php"); $con=new mysqli($hostname,$dbusername,$dbpass,$dbname); if (mysqli_connect_errno($con)) { die('The connection to the database could not be established.'); } $inquired_news=$_POST['news_name']; function newsdetails($inquired_news){ global $con; $querynews="SELECT * FROM news WHERE id='$inquired_news'"; $resultnews=$con->query($querynews); $rownews=$resultnews->fetch_array(); echo json_encode($rownews); } newsdetails($inquired_news); ?>
@nafizaalam -- Not sure, PHP isn't my strong suit - post another question.
2

Try setting the dataType to json

Comments

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

1 Comment

Is there any other way to response in ajax call without use "echo" in php?