I'm trying to get JSON data from my CodeIgniter 3 project, where it only throws JSON data like 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 * FROM users");
$row = $query->result_array();
echo json_encode($row);
}
If you access the link, for example, http://192.168.1.5/codeigniter/get_data/users
, it will display the result like 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 an Angular app inside my CodeIgniter project (please guide me if I put my angular app in the right directory, or should I put it in a separate directory).
/htdocs
/codeigniter
/angular-app <-- my Angular app
/application
/system
/user_guide
Then I try to access the data like this. I first created a user.ts
file that declares variables (please guide me again if this is needed):
export class User {
userID: number;
username: string;
}
Then I created a service that gets the 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 I try to open my Angular app using port 4201. And open my browser to http://192.168.1.5:4201
.
It is working when I use Microsoft Edge. But when I use Google Chrome, I get this error:
Failed to load http://192.168.1.5/codeigniter/get_data/users: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.10.101:4201' is therefore not allowed access.