Mr Spartacus
Mr Spartacus

Reputation: 127

How to set up CORS in an AJAX request

I have been working on a personal webapp and have hit a little snag. My API calls only work for some APIs and not for others. For the ones it doesn't work with I will usually get an error message like so.

XMLHttpRequest cannot load https://api.meetup.com/2/cities. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

After doing some research I know it is to do with CORS not being setup. I was wondering if it would be possible to set this up in the client when making an AJAX request. The current way I am doing this is like so

var handleRequest = function(request){
  $.ajax({
  type: "GET",
  url: request,
  success: function(data) {
    var rawJSON = JSON.stringify(data, null, 2);
    editor.setValue(rawJSON);
  },
  dataType: 'json'
});

Upvotes: 1

Views: 361

Answers (2)

dtdesign
dtdesign

Reputation: 46

The server you're trying to access has to grant you permission to access it. An IT admin has to provide you with a URL that grants you permission to hit their external server. The server you are trying to hit has to setup CORS. http://enable-cors.org/

Upvotes: 3

Vladimir M
Vladimir M

Reputation: 4489

According to their docs they support JSONP.

https://www.meetup.com/meetup_api/

This is your way around CORS.

Upvotes: 2

Related Questions