0

Hello here's my json :

[
    {
        "name": "AAAAAA",
        "date": "28-03-2016",
    },
    {
        "name": "BBBBBB",
        "date": "20-12-2016",
    },
    {
        "name": "CCCCCC",
        "date": "09-01-2016",
    },
    {
        "name": "DDDDDD",
        "date": "21-07-2016",
    }
]

My javascript :

var app = angular.module('app', []);
    app.service('service', function($http, $q){
        var deferred = $q.defer();

        $http.get('names.json').then(function(data){
            deferred.resolve(data);
        });

        this.getNames = function() {
            return deferred.promise;
        }
    });
    app.controller('FirstCtrl', function($scope, service, $http) {
        var promise = service.getNames();
        promise.then(function (data) {
            $scope.names = data.data;
            console.log($scope.names);
        }
    );
    $scope.postfunction = function() {
        $http.post('serwerUrl',{'name':name})
        .success(function(data){console.log('data success');
    };
});

HTML :

<tbody>
    <tr ng-repeat="name in names">
        <td>{{name.name}}</td>
        <td>{{name.date}}</td>
        <td><button ng-click="postfunction(name.name)">POST</button></td>
    </tr>
</tbody>

What I want do is when I click the button "POST" name.name post to server. I try $http.post in postfunction(), but I get error "(501 Unsupported method ('OPTIONS'))" and "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin "in console.

1
  • success is deprecated in Angular >= 1.5 Commented Jan 12, 2017 at 10:09

1 Answer 1

1

The server / api does not support the options header, this is CORS. Before each request angular is performing a OPTIONS header which is basically a handshake. It asks the server if the request it wants to do is allowed.

Make sure you read more about CORS and the server implements what is necessary.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.