0

I'm trying to send a post request to my server. when I use postman I receive correct data as belowenter image description here

now I want to send this request from factory in my angular project:

var data = [
        {
            "id": 6155335970855059712,
            "userId": 2,
            "instagramId": "0x4d_ehdi_wm",
            "searchId": "mehdiketabdar",
            "mediaId": null,
            "phoneNumber": "09368640183",
            "name": "mehdi",
            "date": 1467546456287,
            "otherData": [
            ]
        }
];

var req = {
        method: 'POST',
        url: 'http://atitec.ddns.net:2500/admin_api/',
        headers: {
            'Content-Type': 'application/json'
        },
        data: data
    };
request.url = req.url + "mGetListOfOrders";
$http(request);

but I see the error below in my console:

OPTIONS http://atitec.ddns.net:2500/admin_api/mGetListOfOrders net::ERR_EMPTY_RESPONSE

  • 1
    Looks like you're falling foul of an un-handled [pre-flight check](http://stackoverflow.com/questions/8685678/cors-how-do-preflight-an-httprequest). – Ankh Jul 15 '16 at 08:34
  • @Ankh I think it's not the problem. because CORS problem solved and I inserted the code piece for solving that into server code – MohammadJavad Seyyedi Jul 15 '16 at 08:43

2 Answers2

1

I suppose you need to stringify your data:

var req = {
    method: 'POST',
    url: 'http://atitec.ddns.net:2500/admin_api/',
    headers: {
        'Content-Type': 'application/json'
    },
    data: JSON.stringify(data)
};
request.url = req.url + "mGetListOfOrders";
$http(request);
chf
  • 743
  • 4
  • 14
0

The Simplest way to send request is through a factory. Create a RestFactory.js file and try to do like mentioned below.

var commonFactories = angular.module('commonFactories', []);

commonFactories.factory('yourFactoryApiName', function($http) {

  var anyObj = {};
  yourFactoryApiName.yourMethodName = function(dataObj) {
     return $http.post('yourAPIURL', dataObj);
  }
return anyObj;
)};

Inject the commonFactories dependency to ur app.js and use the service anywhere in your project. If you could make it dynamic for all POST Api sending, even better.

Vivek
  • 36
  • 4