0

I made some javascript to get the object from the api. Now the error show that I dont have access control? what should I do?

<script type="text/javascript">
    function getLatest(){
      var data = {};
      $.ajax({
          url: 'http://api.rappler.com/index.php?option=com_rappler&task=mobileapi.getarticlelist&limit=5&catid=23',
          data: data
        });
      console.log(data);
    }
</script>
treb
  • 478
  • 1
  • 7
  • 16
  • where is your sucess function? – madalinivascu Sep 19 '16 at 05:54
  • `api.rappler.com` does not allow Cross Origin Resource Sharing (CORS) – Jaromanda X Sep 19 '16 at 05:55
  • 1
    Read a little about Cross-site scripting (XSS), when you need a resource from another site/api you need to add Access-Control-Allow-Origin header in your request – Taj Ahmed Sep 19 '16 at 05:55
  • `you need to add Access-Control-Allow-Origin header in your request` - @TajAhmed - perhaps you need a refresher in how CORS works ... `Access-Control-Allow-Origin` is a response header - and whoever upvoted your comment also needs a refresher course in CORS – Jaromanda X Sep 19 '16 at 05:56
  • @TajAhmed: No, the website needs to add Access-Control-Allow-Origin header to its reply, not you to the request you make – slebetman Sep 19 '16 at 07:38

1 Answers1

0
Try this one:

<script type="text/javascript">
    function getLatest(){
      var data = {};
      $.ajax({
          url: 'http://api.rappler.com/index.php?option=com_rappler&task=mobileapi.getarticlelist&limit=5&catid=23',
          data: data,
          async:true,
          dataType : 'jsonp',   //you may use jsonp for cross origin request
          crossDomain:true
        });
      console.log(data);
    }
    getLatest();
</script>
Rax Shah
  • 531
  • 5
  • 18