I am fairly new to Angular.JS development but have had experience in the past with using PHP, HTML and JS so I am currently building a web application with Angular.JS as the frontend and PHP Slim as the API backend. I have been successful in implementing token-based authentication with Angular.JS $http.post for one of my calls but cannot seem to get the desired result in another $http.post request, which does not require the token authentication.
REQUEST 1 - Angular.JS (WORKING):
function ($scope, $http, $location) {
console.log("REACHED");
$scope.master = {};
$scope.activePath = null;
$scope.New_Customer = function(customer, AddNewForm) {
console.log(customer);
console.log("2: " + JSON.stringify(customer));
console.log("SCOPE: " + customer.status);
$http({
method: "post",
url: "v1/customers",
headers: {
'authorization': token,
'Content-Type': 'application/x-www-form-urlencoded'
},
data: $.param({first_name: customer.first_name, last_name: customer.last_name, email: customer.email, mobile_number: customer.mobile_number, home_number: customer.home_number, address_line1: customer.address_line1, address_line2: customer.address_line2, city: customer.city, postalcode: customer.postalcode, 'status': customer.status})
}).success(function(result){
console.log(result);
$scope.reset();
$scope.activePath = $location.path('#/Customers');
});
$scope.reset = function() {
$scope.customer = angular.copy($scope.master);
};
$scope.reset();
};
}
PHP (WORKING)
$app->post('/customers', 'authenticate', function() use ($app) {
// check for required params
verifyRequiredParams(array('first_name'));
$response = array();
$first_name = $app->request->post('first_name');
$last_name = $app->request->post('last_name');
$email = $app->request->post('email');
$mobile_number = $app->request->post('mobile_number');
$home_number = $app->request->post('home_number');
$address_line1 = $app->request->post('address_line1');
$address_line2 = $app->request->post('address_line2');
$city = $app->request->post('city');
$postalcode = $app->request->post('postalcode');
global $user_id;
$db = new DbHandler();
// creating new customer
$customer_id = $db->createCustomer($user_id, $first_name, $last_name, $email, $mobile_number, $home_number, $address_line1, $address_line2, $city, $postalcode);
if ($customer_id != NULL) {
$response["error"] = false;
$response["message"] = "Customer created successfully";
$response["customer_id"] = $customer_id;
echoRespnse(201, $response);
} else {
$response["error"] = true;
$response["message"] = "Failed to create customer. Please try again";
echoRespnse(200, $response);
}
});
However, for my login script, the data does not appear to be reaching the PHP file where the $http.post request always returns 'null' in the console.
REQUEST 2 - Angular.JS (NOT WORKING)
function ($scope, $http, $location) {
console.log("REACHED");
$scope.Login = function(customer, LoginForm) {
console.log(customer);
console.log("2: " + JSON.stringify(customer));
console.log("SCOPE: " + customer.email);
$http({
url: "v1/login",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({email: customer.email, password: customer.password})
}).success(function(data, status, headers, config) {
$scope.data = data;
console.log($scope.data);
}).error(function(data, status, headers, config) {
$scope.status = status;
$scope.data = data;
console.log($scope.status + " " + $scope.data);
});
};
}
PHP (NOT WORKING)
$app->post('/login', function() use ($app) {
// check for required params
verifyRequiredParams(array('email', 'password'));
$response = array();
$email = $app->request()->post('email');
$password = $app->request()->post('password');
$db = new DbHandler();
// check for correct email and password
if ($db->checkLogin($email, $password)) {
// get the user by email
$user = $db->getUserByEmail($email);
if ($user != NULL) {
$response["error"] = false;
$response['name'] = $user['name'];
$response['email'] = $user['email'];
$response['apiKey'] = $user['api_key'];
$response['createdAt'] = $user['created_at'];
} else {
// unknown error occurred
$response['error'] = true;
$response['message'] = "An error occurred. Please try again";
}
} else {
// user credentials are wrong
$response['error'] = true;
$response['message'] = 'Login failed. Incorrect credentials';
}
echoRespnse(200, $response);
});
Console log:
app.js:148 REACHED
app.js:148 REACHED
/contacts_app_2/#/Login:1 POST http://localhost/contacts_app_2/ 412 (Precondition Failed)
Navigated to http://localhost/contacts_app_2/
app.js:148 REACHED
app.js:148 REACHED
app.js:151 Object {email: "[email protected]", password: "testpassword123"}
app.js:152 2: {"email":"[email protected]","password":"testpassword123"}
app.js:154 SCOPE: [email protected]
app.js:167 0 null
Navigated to http://localhost/contacts_app_2/
app.js:148 REACHED
app.js:148 REACHED
I cannot seem to figure out why my first request (to create a new customer works) yet to login does not work. Any help with this would be greatly appreciated as I have spent time looking through the forums for other answers but have not yet found a way to resolve this issue.
echoRespnse(200, $response);.