1

when i am testing api with rest client postman, its working fine. This is the parameter.

URL: http://localhost/api2/v1/delete_item_in_order
Type : post
Body : order_id:1091, product_id:12, variant_id:20

Same thing i want to with service and controller in angularjs.

Service :

var BASEURL = 'http://localhost/api2/v1/';
sampleApp.factory('Order', function ($resource, $cacheFactory) {
  return $resource(BASEURL + 'shop_orders/:id', {id: '@id'}, {
    'query': {method: 'GET', cache: true},
    'get': {method: 'GET'},
     'deleteOrderRow' :{
      method : 'POST',
      url : BASEURL + 'delete_item_in_order',
      params : { order_id : '@order_id', product_id : '@product_id', variant_id : '@variant_id' }
    }

  });
});

Controller:

Order.deleteOrderRow({order_id: od.order_id, product_id: od.product_id, variant_id: od.varient_id}, function (data) {
     alert('success');
});

But this is giving error in console:

angular.js:10765 OPTIONS http://localhost/api2/v1/delete_item_in_order?order_id=1091&product_id=767&variant_id=47 (anonymous function) @ angular.js:10765r @ angular.js:10558g @ angular.js:10268(anonymous function) @ angular.js:14792r.$eval @ angular.js:16052r.$digest @ angular.js:15870r.$apply @ angular.js:16160(anonymous function) @ angular.js:23618If @ angular.js:3346Hf.d @ angular.js:3334

/#/GetDetails/1091:1 XMLHttpRequest cannot load http://localhost/api2/v1/delete_item_in_order?order_id=1091&product_id=767&variant_id=47. Response for preflight has invalid HTTP status code 404

Can anyone help me on this? What is wrong and what changes is needed in controller and/or service to make the request successful.

Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131
  • 1
    Preflight request errors generally mean you don't have CORS set up properly. – Claies Jun 09 '16 at 01:21
  • Are you running your Angular app from `http://localhost`, `file:///` or something else? – Phil Jun 09 '16 at 01:22
  • It is with http:// partner.localhost, basiclayy it is with subdomain – Devesh Agrawal Jun 09 '16 at 01:25
  • Different domain means CORS required. – Phil Jun 09 '16 at 01:26
  • But it is same domain. no?? – Devesh Agrawal Jun 09 '16 at 01:27
  • No, `partner.localhost` <> `localhost` – Phil Jun 09 '16 at 01:27
  • but other requests are working fine. Like 'query': {method: 'GET', cache: true}, i.e getting the data. – Devesh Agrawal Jun 09 '16 at 01:29
  • At the very least, your API is not configured correctly for a CORS request to that URL. The `OPTIONS` (pre-flight) request is returning a 404 status. – Phil Jun 09 '16 at 01:45
  • Also, are you sure you want to `POST` that data as query params. Your Postman example seems to put the data in the request body. To do so, remove the `params` object from your `$resource` action definition – Phil Jun 09 '16 at 01:49
  • Your factory definition has to have an object and is incorrect. A factory value is created everytime and thats conflicting I believe. Change the $resource to a named method and it should work. – Gary Jun 09 '16 at 02:09

0 Answers0