This worked for us: header ('Access-Control-Allow-Origin: *');
Add this to the controller method before returning for one endpoint to the constructor of the controller class for all endpoints in this branch or higher to allow it on the site.
If you allow certain sites to access the usage header ("Access-Control-Allow-Origin: example.com") or something like the header ("Access-Control-Allow-Origin:". $ Remote_domain). Where $ remote_domain is set dynamically based on some transferred in the token or such. Check out Cross-Resource Access (CORS) to limit your use of * wildcard characters.
<?php class Say { __construct(){ header('Access-Control-Allow-Origin: *'); //Here for all /say } function hello($to='world') { header('Access-Control-Allow-Origin: *'); //Here for just /say/hello return "Hello $to!"; } }
The above works for GET and POST, other actions require some additional header information from the restler. Here are some examples:
header ('Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS'); header ('Access-Control-Allow-Headers: whatever_headers_you_allow, header1, header2');
For IE9 and below, you will need a JSONP hacker. Restler has an example of extending the iFormat class to port the JASONP interface to an API.
Check out Mozilla hacks for more information on CORS. http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ And check the OPTIONS in the PHP REST API
source share