Guillermo Alvarez
Guillermo Alvarez

Reputation: 1775

Simple way to integrate php api with angular 2 application on local machine

I am creating a small Angular 2 application with a php based REST API. The way I have been developing it so far is I created mock data in a json file and used that to create the front end. I created the php REST API using MAMP and tested it separately and it works fine. The problem I am having is I would like to test the integration of both of them but when I run the npm lite server and try to make an HTTP request to the API it doesn't work. Is there a npm server I can use instead of the lite-server that would run the php API? Or is there another solution I am completely overlooking?

Upvotes: 1

Views: 715

Answers (2)

sitesbyjoe
sitesbyjoe

Reputation: 1861

You can also just do it in your PHP file so you don't have to fiddle with apache:

<?php
header('Access-Control-Allow-Origin: *');
header('ContentType: application/json');
echo json_encode('whatever');
exit;
?>

Upvotes: 0

Guillermo Alvarez
Guillermo Alvarez

Reputation: 1775

I actually found one solution. So I can enable CORS on my local MAMP server by editing the Apache configuration file. In case anybody needs to do something similar here is a very short tutorial on how to do the same:

http://enable-cors.org/server_apache.html

This works great for my needs.

Upvotes: 1

Related Questions