Angularjs bad request URL - angularjs

I have a problem with the wrong URL in Request URL. I have api in Laravel, the address to him is: api.shop.local - this is vhost. When I send a request from angular using POST, in the Network tab in the console, in RequestURL there appears: client.shop.local instead of api.shop.local.
What I doing wrong?
This is my code:
JS
$scope.login = function (data) {
console.log(data);
$http({
method: 'POST',
url: 'authenticate',
data: {email: data.username, password: data.password}
}).then(
function (res) {
console.log('succes !', res.data);
},
function (err) {
console.log('error...', err);
}
);
};
And routes in api Laravel:
Route::group(['prefix' => 'api'], function()
{
Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
Route::post('authenticate', 'AuthenticateController#authenticate');
});

Related

sending data with post request in angular and handling that data in node js

I was trying a post request in angular and i had to send some data with it but i am unable to handle that data at the back-end Nodejs or may i am not sending the data right way.
I need help!
$scope.sendData = function() {
$scope.obj = {
username: $scope.username,
password: $scope.password
};
console.log($scope.obj);
$http({
method: 'POST',
url: "/upload",
data: $scope.obj,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
};
app.post('/upload', function(req, res) {
console.log(req.body);
res.send("hello");
});
when i console log req.body i get the data but in very weird format:
help please
You need to stringify $scope.obj before sending to the server.
$scope.obj = JSON.stringify($scope.obj);
Add this line just below your console.log statement.

Passing $scope through AJAX, but only few variables inserted

I am trying to pass a variable through AJAX to an API. Here is the angular controller:
$scope.register = function() {
_.each($scope.photos, function(images) {
$upload.upload({
url: '/api/indorelawan/timaksibaik/register/upload-images',
method: 'POST',
data: {},
file: images
})
.success(function(data) {
$scope.team.photos.push(data.result.path);
})
});
$http({
method : 'POST',
url : '/api/indorelawan/timaksibaik/register',
data : $.param($scope.team),
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function(data) {
if (!data.success) {
...
}
else {
...
}
});
}
I tried console.log the $scope.team.photos before it calls the /register API. It displays the data perfectly. But when /register API is runned, the $scope.team.photos is not included. Here is the API:
/*Register Tim Aksi Baik*/
apiRouter.post('/timaksibaik/register', function(req, res) {
// TODO: Create new value to access general statistics data, e.g.: response time.
console.log(req.body);
var team = new GoodActionTeam();
_.each(req.body, function(v, k) {
team[k] = v;
});
team.created = new Date();
team.save(function(err, data) {
if (err) {
res.status(500).json({
success: false,
message: "Gagal menyimpan data organisasi baru.",
system_error: "Error while saving organization data: " + err.message
});
}
else {
res.status(200).json({
success: true,
message: "Organisasi Berhasil Dibuat",
result: data
});
}
});
});
The output of the req.body is only:
{ logo: '/uploads/user_avatar/register/2018-1-14_18:18:3.png',
name: 'ererr',
url_string: 'ererr',
description: 'dfdfd',
focuses: [ '549789127e6a6e2c691a1fc0', '549789127e6a6e2c691a1fc0' ] }
It looks like the $scope.team.photos is not included when the data is passed to the API. What went wrong?
The $upload.upload() is async and by the time you make a post with $scope.team there is no guarantee that all the upload success callbacks have been completed

Express redirect sending page as data to success of angular $http instead of redirecting to the page [duplicate]

This question already has answers here:
How to manage a redirect request after a jQuery Ajax call
(34 answers)
Closed 7 years ago.
When the user is validated the client get the page contents of home.html in result instead of redirecting to the home.html.
Client side call:
$http({
method: "post",
url: "http://localhost:2222/validateUser",
data: {
username: $scope.username,
password: $scope.password
}
}).then(function (result) {
if (result.data && result.data.length) {
alert('User validated');
} else {
alert('invalid user');
}
});
Server side controller method:
module.exports.validateUser = function (req, res) {
User.find({ 'username': req.body.username, 'password': req.body.password }, function (err, result) {
if (result.length) {
req.session.user = result[0]._doc;
res.redirect('/home');
}else{
res.json(result);
}
});
};
Route in app.js:
app.get('/home', function (req, res) {
var path = require('path');
res.sendFile(path.resolve('server/views/home.html'));
});
You could move your redirect logic to the client.
Client:
$http({
method: "post",
url: "http://localhost:2222/validateUser",
data: {
username: $scope.username,
password: $scope.password
},
}).then(function (result) {
alert('user validated');
window.location.replace('/home');
}).catch(function(result) {
alert('login failed');
});
Server:
module.exports.validateUser = function (req, res) {
User.find({ 'username': req.body.username, 'password': req.body.password }, function (err, result) {
if (result.length) {
req.session.user = result[0]._doc;
res.send('OK');
} else {
// responding with a non-20x or 30x response code will cause the promise to fail on the client.
res.status(401).json(result);
}
});
};

Nodejs post request

I am trying to login to a website with the following request:
var request = require('request');
var options = {
method: 'POST',
url: 'https://server/EnterpriseController',
params: {a: 1},
form: "actionType=authenticateUser&reqObj=[null,username,password,null,1]",
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
withCredentials: true,
rejectUnauthorized: false
};
request(options,
function (error, response, body, data) {
if (request.method === 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
var post = qs.parse(body);
});
}
console.log(body);
}
);
I am always getting an error. I think the form is wrong but I have the same login on an angularjs site without any error. I don't understand why the login works on angularjs site but not in nodejs.
)]}',
{"login_err": true, "resp": [null,null,null,1]
}
I missed the cookie:/ And the Formdata should be an Object.

Is there such a thing as a finally with AngularJS $http?

I have code like this:
$http({
method: 'POST',
url: "/api/Account/Register",
data: {
userName: userName,
password: password,
confirmPassword: confirmPassword
}
})
.success(function () {
successCallback();
})
.error(function (data) {
errorCallback(data);
})
Is there any way that I could add in a finallyCallback to this with AngularJS ?
Yes, there is a finally method since 1.2.0rc1, as shown by the documentation. The method was known as always since 1.1.5.
$http({
method: 'POST',
url: "/api/Account/Register",
data: {
userName: userName,
password: password,
confirmPassword: confirmPassword
}
})
.success(successCallback)
.error(errorCallback)
.finally(finallyCallback)
;
The promise returned by $http is the same as any other promise created with the $q service. That means it will have a finally method:
$http({ /* ... */ })
.success(function () {})
.error(function () {})
.finally(function () {});
From the $http docs:
Returns a promise object with the standard then method and two http specific methods

Resources