1

I'm using AngularJs and php, I've created a php file that returns Json format, but when it comes to reading it from my js file, the output is the php code !

The server works fine with php and the php file is exectuded perfectlly and return json format but when i call it in the app the output is my php text witch meens that the php file is reeded as a text.

What should I do to make it readable as json ?

This is my anjularJs call:

$http.get( './templates/connect.php').success(
function(data){
  $scope.cityes=data;  
  alert($scope.cityes); 
});

And this is my php file:

$qry = mysql_query('SELECT capital from countries');
$emparray = array();
while ($rows = mysql_fetch_array($qry)) {
    $emparray[] = $rows;
}
header('Content-type:application/json;charset=utf-8');
echo json_encode($emparray);
5
  • Does control goes in success function ? Did you try with alert or console.log ? Commented Jul 15, 2016 at 20:16
  • 1
    Sure your server processes php? Commented Jul 15, 2016 at 20:16
  • You should view the PHP file itself, and check for (white)spaces at the beginning or the end of the content. Make sure the json object is the only content on the page. Commented Jul 15, 2016 at 20:23
  • Assuming your php file is correct and there are no other problems with the request, use some debuging in your javascript file. Also I don't remember if $.get from jquerry automatically converts the data to json. You could try $.getJSON('<host>', function (data) { console.log(data); }); or use $.ajax standard with specifying the dataType, I always use $.ajax although you write more you have more control. Cheers Commented Jul 15, 2016 at 20:23
  • The server works fine with php and the php file is exectuded perfectlly and return json format but when i call it in the app the output is my php text witch meens that the php file is reeded as a text Commented Jul 15, 2016 at 21:09

1 Answer 1

1

Make sure your PHP file starts with a <?php and the PHP processor is enabled in your server:

<?php
$qry = mysql_query('SELECT capital from countries');
$emparray = array();
while ($rows = mysql_fetch_array($qry)) {
    $emparray[] = $rows;
}
header('Content-type:application/json;charset=utf-8');
echo json_encode($emparray);

More about the PHP syntax and its opening/closing tags.

Sign up to request clarification or add additional context in comments.

1 Comment

Be careful with Json headers and IE, well anything with IE is just.. yuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.