CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Use Ajax API with website on different domain
Pages: [1]

Author Topic: Use Ajax API with website on different domain  (Read 832 times)

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Use Ajax API with website on different domain
June 28, 2013, 06:00:44 am
Hi guys,

is it possible to use the ajax api on an external website? The wiki (http://wiki.civicrm.org/confluence/display/CRMDOC43/API+Reference#APIReference-AJAX) says
Quote
The AJAX interface may be adaptable to use with [...] web-pages hosted on the same domain
What about websites on different domain from the crm? I've tried the following script within a website on an external website (e.g. on http://www.notthecrmdomain.com/crmajaxtest.html)

Code: [Select]
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.mycrmdomain.com/sites/all/modules/civicrm/js/rest.js?r=RPmIS"></script>
<script type="text/javascript">
CRM.api('Event','get',
  {
   
  },
  {
    success: function(data) {
      window.alert('Found ' + data.count + ' item(s)');
    }
  }
);
</script>

No data is returned because of an 404 server error. The script sends the request to "http://www.notthecrmdomain.com/civicrm/ajax/rest?entity=Event&action=get&json=1" instead of "http://www.mycrmdomain.com/civicrm/ajax/rest?entity=Event&action=get&json=1". Is it possible to specify the target crm url?

Philipp

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Use Ajax API with website on different domain
June 28, 2013, 08:01:41 am
Not possible, it's a basic security feature of ajax (same origin policy)
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: Use Ajax API with website on different domain
July 02, 2013, 07:15:26 am
I know about this restriction. I've ended up with a php "proxy" file (http://benalman.com/projects/php-simple-proxy/, slightly modded), which makes a curl request to the rest api and returns the json result as jsonp. Additionally I had to change the ajax-url in rest.js:

Code: [Select]
ajaxURL: 'path/to/simple-proxy.php?url=http://demo.civicrm.org/sites/all/modules/civicrm/extern/rest.php'
Now I can make cross-domain requests to the civiCRM api.
« Last Edit: July 02, 2013, 07:19:05 am by PhilippMikael »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Use Ajax API with website on different domain
July 02, 2013, 07:21:55 am
Hi,

You might want to double check that your proxy isn't opening more than you'd want to. ie can it be used to delete contacts? read contributions? send emails? ...

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

pminf

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Drupal 7
  • MySQL version: 5.6
  • PHP version: 5.3
Re: Use Ajax API with website on different domain
July 02, 2013, 07:28:07 am
Thank you for your tip. Of course you have to pass the key and api_key to get some data. And there is only one user, who has access to CiviCRM via the api. Additionally this user just has permissions to read event information. So you can not access any data, which isn't public anyways. I guess this is kinda secure, right?

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Use Ajax API with website on different domain

This forum was archived on 2017-11-26.