I have the following piece of code in my ionic app:
var req = {
method: 'PUT',
dataType: "json",
url: loginservice.getServerAPI()+'/restaurants/'+$scope.currentRu.id+'/menu',
data: response,
headers: {
"Content-Type" : "application/json",
"Authorization" : "Bearer "+loginservice.gettoken()
}
};
$http(req)
.success(function (data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
$state.go('wtf.thanks');
return data;
})
.error(function (data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(data);
return "error";
});
};
Each time, I get an error that is different each time while the request is exactly the same.
Here is the related API code:
routes/restaurants.js:
router.route('/:id/menu')
.put(restaurantController.updateRestaurantMenu);
controllers/restaurant.js:
exports.updateRestaurantMenu = function(req, res) {
RestaurantModel.findOne({'id': req.params.id}, function(err, restaurant) {
if (!err) {
// update restaurant queue
restaurant.menus = req.body.menus;
restaurant.save(function(err) {
if (!err) {
return res.send(restaurant);
} else {
console.log(err);
return res.status(400).send(err);
}
});
} else {
console.log(err);
return res.status(400).send(err);
}
});
};
All I get in my logs is:
...
OPTIONS /api/restaurants/174/menu 200 1.742 ms - 3
OPTIONS /api/restaurants/174/menu 200 0.928 ms - 3
...
Instead of something like:
...
OPTIONS /api/restaurants/174/menu 200 1.742 ms - 3
PUT /api/restaurants/174/menu 200 1.742 ms - 3
OPTIONS /api/restaurants/174/menu 200 1.742 ms - 3
PUT /api/restaurants/174/menu 200 0.928 ms - 3
...
CORS is handled.
app.js:
// allow cross domain
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization');
next();
};
app.use(allowCrossDomain);
As for now, it is the only PUT
request that behaves this way. I have other PUT
requests that work just fine.
Okay, so the problem was due to the send of a token (for Basic Auth purposes) that was way too big apparently. This was due to a problem in the code but wasn't spotted before because the token wasn't big enough before to break things.