0

This is the front-end part

$scope.result = {};

$scope.edit = function() {
  console.log($scope.result); //This is working
  $http.put('http://localhost:4000/customer/' + $routeParams.id, $scope.result)
    .then(function(data) {
      $scope.result = data;
      console.log("posted successfully") //This is not working
    })
    .catch(function(err) {
      console.log(err);
    });
}

Catching at .catch every time

SiddAjmera
  • 32,111
  • 5
  • 45
  • 85

1 Answers1

0

OPTIONS calls are generally made on their own to check if an API allows CORS for requests coming in from a particular domain.

I'm pretty sure what's happening here is, when you're making the PUT call, $http is first making the OPTIONS call to see if the API supports CORS. But since it doesn't, it's returning an Error. This error is getting caught in your .catch block.

If this was a GET request, you could have used JSONP instead of GET(considering the API supported JSONP requests).

But since this is not, you can do two things here:

  1. Get your Domain whitelisted on the API if you have a control on the API.
  2. Create a proxy which will relay your request to that API. Since this proxy will be treated as a server and not a client, and since CORS is only a browser-based policy, your request will go through successfully.

Hope this makes sense.

SiddAjmera
  • 32,111
  • 5
  • 45
  • 85