php - Angular - Get JSON data from CodeIgniter -


i'm trying json data codeigniter 3 project, throws json data this:

class get_data extends ci_controller {      public function __construct()     {         parent::__construct();         $this->load->database();         $this->load->library('session');         $this->load->helper('url_helper');     }      public function users()     {         $query = $this->db->query("select * users");         $row = $query->result_array();          echo json_encode($row);     } 

if access link, example, http://192.168.1.5/codeigniter/get_data/users, display result this:

[{"userid":"2","username":"3-d"},{"userid":"3","username":"3com"},{"userid":"4","username":"3d"},{"userid":"5","username":"3kva"},{"userid":"6","username":"3m"},{"userid":"7","username":"a4tech"}]

and i've created angular app inside codeigniter project (please guide me if put angular app in right directory, or should put in separate directory).

/htdocs     /codeigniter         /angular-app <-- angular app         /application         /system         /user_guide 

then try access data this. first created user.tsfile declares variables (please guide me again if needed):

export class user {     userid: number;     username: string; } 

then created service gets data:

@injectable() export class userservice {      constructor(private http:http) {     }      getusers() {          return this.http.get('http://192.168.1.5/codeigniter/get_data/users')                .map(res => res.json());     } } 

then try open angular app using port 4201. , open browser http://192.168.1.5:4201.

it working when use microsoft edge. when use google chrome, error:

failed load http://192.168.1.5/codeigniter/get_data/users: no 'access-control-allow-origin' header present on requested resource. origin 'http://192.168.10.101:4201' therefore not allowed access.

you need add below code request headers.

 this.headers.append('access-control-allow-origin', '*'); 

and condeingiter controller add below api is

header("access-control-allow-methods: get, options"); 

https://enable-cors.org/server_php.html


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -