mardi 9 août 2016

Ember ajax DELETE call from different port getting " No 'Access-Control-Allow-Origin' header is present on the requested resource"

I have an Ember application and I am trying to do a DELETE request on 'click' from a component (sent to the route). The server is on a different port than the User interface.

This is the ajax request on the route:

         deleteAlert(alert) {
            let self = this;
            Ember.$.ajax({
                url: 'http://ift.tt/2aJ4nvX' + alert.id,
                type: 'DELETE',
                beforeSend: function(request)
                {
                    request.setRequestHeader("Access-Control-Allow-Origin","example.com:4301");
                    request.setRequestHeader("Access-Control-Allow-Methods","GET,POST,OPTIONS,PATCH,DELETE,PUT");
                },
                success: function (response) {
                    if(response.status==='Success!') {
                        Ember.Logger.log(response);
                    }
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    Ember.Logger.error(textStatus, errorThrown, jqXHR);
                }
            });

I am getting the following error, however:

XMLHttpRequest cannot load http://ift.tt/2aHQhaM. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com:4301' is therefore not allowed access. The response had HTTP status code 404.

I have also tried adding "*" to the 'Access-Control-Allow-Origin' header as well as defining the headers in the following way (outside the beforeSend function):

          deleteAlert(alert) {
            let self = this;
            Ember.$.ajax({
                url: 'http://ift.tt/2aJ4nvX' + alert.id,
                type: 'DELETE',
                headers: {
                    'Access-Control-Allow-Origin':'http://example.com:4301',
                    'Access-Control-Allow-Methods':'GET,POST,OPTIONS,PATCH,DELETE,PUT',
                },
                success: function (response) {
                    if(response.status==='Success!') {
                        Ember.Logger.log(response);
                    }
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    Ember.Logger.error(textStatus, errorThrown, jqXHR);
                }
            });

Same error.

Is this something that needs to be fixed on the server-side? I have no problems with POST or GET requests, just DELETE so far.

Any help is appreciated!




Aucun commentaire:

Enregistrer un commentaire