0

I'm developing a webapp and I'm using Angunlar 2 as my frontend framework and CodeIgniter 3 as my backend framework. I use Angular-CLI, so my Angular app runs on port 4200, while my "server-side" works on port 80.

So, when I try to do a post request from Angular to PHP, I'm making a cross-domain request, because of the different ports. I checked it in the Network tabs of Chrome, I see the two request done (one for options and one for the post itself).

The problem that I have is that I can't figure out to receive the post body (I have changed the Content-Lenght to in my header and is always on runtime).

The headers I have configured are:

header('Content-Type: text/plain; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Content-Type');

The question is: is there a way to avoid the cross-domain here? Naturally, I have tryed to start the serve in port 80, but it is busy. And if there is not a way to avoid the cross-domain, could someone please give me and idea of how to continue working with this?

Thanks

  • I have to mention thtat I don't have problems with get request, the problem that I have is with the post request and the problem is that I don't receive anything in the body (the content-lenght is 2 and I can't change it) –  Jun 28 '17 at 14:09
  • For those who have the same problem, the solution that @Raiper34 wrote is perfect. If you still having problems, maybe is because the $_POST array is not populating. The solution to this is here: https://stackoverflow.com/a/14794856/3368457 –  Jun 28 '17 at 15:28

1 Answers1

0

You can run "google-chrome --disable-web-security --user-data-dir" or ude this plugin https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi , because server side settings is not enought, you need to send modified api calls too.

Raiper34
  • 537
  • 2
  • 6
  • 20
  • Perfect! I tryed it and I can do the requests as if they are in the same domain –  Jun 28 '17 at 14:22
  • Althought, I still not receiving the body of the request. I try in the server-side this: `echo json_encode($_POST]);` to see if I receive something but nothing... –  Jun 28 '17 at 14:25
  • do you mean on server side? – Raiper34 Jun 28 '17 at 14:27
  • Yes. I did that to check if I receive something but still nothing –  Jun 28 '17 at 14:28
  • And did you see, that data was send in Chrome developer console? – Raiper34 Jun 28 '17 at 14:33
  • Yes, the data is sent, I see the request payload correctly –  Jun 28 '17 at 14:34
  • Try it only with this line: header("Access-Control-Allow-Origin: *"); – Raiper34 Jun 28 '17 at 14:38
  • The only thing I can think is wrong is the content-length header, the server-side is always 4 and naturaly, the client-side depends on the data. I have tryed to set it but no lucky –  Jun 28 '17 at 14:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147842/discussion-between-user3368457-and-raiper34). –  Jun 28 '17 at 14:41