cross domain rest api post put get delete

2019-08-03 06:35发布

问题:

I built a REST api with GET, POST, PUT, DELETE for the angularjs single page application . Now my client wants to host REST api in separate domain and angularjs single page application in another domain. Here at development both angularjs application and REST api in same domain but if i move REST api to another domain my calls to REST api from angularjs application comes under cross domain requests, I tried with JSONP for call requests but only get requests are working and POST, PUT, DELETE requests are not working.

How do i solve this problem to make POST, PUT, DELETE requests with REST api that is in different domain.

回答1:

If you have access to modify REST api you could try to add this code at the top.

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');

You could change "*" to your AngularJS application's domain name. I hope that could help you.