跨域其余api发布放置删除 [英] cross domain rest api post put get delete

查看:55
本文介绍了跨域其余api发布放置删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为angularjs单页应用程序构建了一个具有GET,POST,PUT,DELETE的REST api.现在,我的客户希望将REST api托管在单独的域中,并将Angularjs单页应用程序托管在另一个域中.在开发中,angularjs应用程序和REST API在同一域中,但是如果我将REST api移至另一个域,我从angularjs应用程序对REST api的调用属于跨域请求,则我尝试使用JSONP进行调用请求,但只有get请求有效且POST,PUT,DELETE请求不起作用.

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.

我该如何解决此问题,以便使用不同域中的REST api进行POST,PUT,DELETE请求.

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

推荐答案

如果您有权修改REST api,则可以尝试在顶部添加此代码.

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');

您可以将"*"更改为AngularJS应用程序的域名.希望对您有所帮助.

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

这篇关于跨域其余api发布放置删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆