I am trying to make a POST request from my Ionic Project in angular to my Laravel Dingo API. When I make the POST request in POSTMAN, it successfully creates a new record, but when I do it in Angularjs, it returns the response for the GET request.
This the response POSTMAN suggests for the code for Jquery:
var settings = {
"async": true,
"crossDomain": true,
"url": "http://app.extremenazarene.org/api/contacts",
"method": "POST",
"headers": {
"authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2FwcC5leHRyZW1lbmF6YXJlbmUub3JnXC9hcGlcL2xvZ2luIiwiaWF0IjoxNDY0NjM1NDQwLCJleHAiOjE0NjQ2MzkwNDAsIm5iZiI6MTQ2NDYzNTQ0MCwianRpIjoiYmZjMTc3YzdhODk5OGE1Y2Q1NWRiYjIzOTU4YzQ5YzMifQ.fdiGmKy9ipPnvdLuapFFe8Rz6nD7ty-gkzfWq8ySO_U",
"cache-control": "no-cache",
"postman-token": "c022a35b-bbdd-2f77-f7b9-2802340dd0bb",
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"fname": "sarkinda"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
My angularjs request is
storeContact: function(token, data) {
var deferred = $q.defer();
var promise = deferred.promise;
var settings = {
method: "POST",
url: "/api/api/contacts/",
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' +token
},
data: data
};
console.log(settings);
$http(settings).then(function mySuccess(response) {
deferred.resolve(response.data);
}, function myError(response){
deferred.reject(response.statusText);
});
promise.success = function(fn) {
promise.then(fn);
return promise;
};
promise.error = function(fn) {
promise.then(null, fn);
return promise;
};
return promise;
},
The angularJS code returns a success response and returns data from the same URL for a GET request. I believe all my Laravel code is right because POSTMAN can successfully run the call and create a new record. There must be something wrong with my angularJS code.
Note I have also tried sending data through angular js formated like this:
data: {'fname':'testname'}
application/jsonand try again - Shashank Jain$data = file_get_contents('php://input');to get at the data instead of using$_POST. - swatkins