Get data from webhost

hi every one i have project where i need to get data from server
i’m using 000webhost i have the code here

    this.http.get('http://daleel-ksu.000webhostapp.com/feed.php', {}, {})
    .then(data => {
  
      console.log("d"+data);
 
  
    })  .catch(error => {
  
      console.log(error);
     
  
    });

but the problem that i get nothing even i dont get any error do i need to use api and how i can i use it
here is my php

<?php 
$mysqli = new mysqli("localhost", "h", "j", "data");
$query = "SELECT * FROM daleel";
$dbresult = $mysqli->query($query);

while($row = $dbresult->fetch_array(MYSQLI_ASSOC)){

    $data[] = array(
        'id' => $row['NID'],
        'name' => $row['name']
    );
}

if($dbresult){
    $result = "{'success':true, 'data':" . json_encode($data) . "}";              
}
else {
    $result = "{'success':false}";
}



echo($result);
?>


please help it’s urgent

Are there errors reported in the console? Are all the Angular modules imported?

Where are you testing/running it?

Also, the JSON it returns appears to be invalid when I pasted it in JSONLint.

i test it on browser i don’t get any error
i tried a new code

import { Headers, Http, Response } from '@angular/http';
 import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';

this is the method

 get() {
    let url ="http://daleel-ksu.000webhostapp.com/feed.php";
    this.http.get(url)
    .map(res => res.json())
    .subscribe(data => {
          console.log(data)
    });
   }

i get this error

core.js:1449 ERROR SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Response.Body.json (http.js:1091)
    at MapSubscriber.project (https.ts:42)
    at MapSubscriber._next (map.js:35)
    at MapSubscriber.Subscriber.next (Subscriber.js:53)
    at XMLHttpRequest.onLoad (http.js:1591)
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (core.js:4751)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)

your json is not correct, look into building up one final result array and then echoing out json_encode($result)

how i can do that i’m new to coding with this

Something like this
instead of
$result = “{‘success’:true, ‘data’:” . json_encode($data) . “}”;

$result = array(‘success’ =>true,‘data’ =>$data);
echo json_encode($result);

See https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script has some more in depth info on returning json from php

thank u so much for ur effort it helps alot
i fix it with this way
first change method get to


  get() {
    let url ="http://daleel-ksu.000webhostapp.com/feed.php";
     return new Promise(resolve => {
      this.http.get(url)
        .map(res => res.json())
        .subscribe((data: any) => {
          resolve(data.Data);
          console.log(data);
        }, error => {
          resolve(error);
          console.log(error);

        });
    });
  }

then change php to

<?php 
$mysqli = new mysqli("localhost", "db userNAme", "db password", "db name");
$query = "SELECT * FROM daleel";
$dbresult = $mysqli->query($query);
header('Access-Control-Allow-Origin: *');
header('Content-type: application/json');
while($row = $dbresult->fetch_array(MYSQLI_ASSOC)){

    $data[] = array(
        'id' => $row['NID'],
        'name' => $row['name']
    );
}

if($dbresult){
$result = array('success' =>true,'data' =>$data);

}
else {
$result = array('success' =>false);

    
}

echo json_encode($result);

?>

thank u alot that link helped alllot

Yes that looks much better

1 Like