如何在PHP脚本中使用angularjs $ resource [英] How to use angularjs $resource with php script
问题描述
我有关于PHP-AngularJs的问题,所以我有简单的PHP脚本
i have question related to PHP - AngularJs, so i have simple PHP script
<?php
require_once '../../dbConnect.php';
$driverId = $_POST['driverId'];
if (isset($_POST['driverId'])) {
$sql = "delete from drivers where driver_id='$driverId'";
if ($mysqli->query($sql) === TRUE) {
echo mysqli_insert_id($mysqli);
} else {
echo "Error updating record: " . $mysqli->error;
}
$mysqli->close();
}
?>
此刻,我将数据传递给这样的脚本
At the moment i pass data to script like this
return $http({
method: 'POST',
url: '/api/drivers/deleteDriver.php',
data: $.param(driverObject),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
我不喜欢该代码,在其他Java项目上,我使用angularjs $ resource服务将参数发送到端点,像这样
And i don't like that code, on other Java project i send params to end points with angularjs $resource service like this
var deleteDriverResouce = $resource('/api/drivers/deleteDriver.php');
function deleteDriver() {
deleteDriverResouce.save(driverObject);
}
正如您所看到的那样,这是更简洁的代码,更易于使用,我想知道我可以使用$ resource服务将对象传递给php脚本吗?
And as you can see that is cleaner code and easier to use, i'm wondering can i use $resource service to pass object to php script ?
推荐答案
所以我找到了解决方案,我将在这里共享它,所以也许有人会需要它.为了使用AngularJs $ resource服务,您只需要在PHP脚本中进行少量更改,只需以这种方式添加 $ object = json_decode(file_get_contents("php://input"),true);
您可以访问通过$ resource发送的对象.这是一个有效的PHP脚本的示例.
So i have found solution and i will share it here so maybe someone will need it. In order to use AngularJs $resource service you just need to make small change in PHP script, just add $object = json_decode(file_get_contents("php://input"), true);
on that way you can access to object that you sent via $resource. And here is example of one working PHP script.
<?php
require_once '../dbConnect.php';
session_start();
$object = json_decode(file_get_contents("php://input"), true);
if (isset($object['email']) && isset($object['password'])) {
$email = $object['email'];
$password = $object['password'];
$query="select * from members where email='$email'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$row = mysqli_fetch_assoc($result);
if($row) {
if (password_verify($object['password'], $row['password'])) {
$_SESSION["id"] = $row['id'];
echo 'Login Success!';
} else {
session_destroy();
var_dump(http_response_code(400));
}
} else {
session_destroy();
var_dump(http_response_code(406));
}
$mysqli->close();
} else {
session_destroy();
var_dump(http_response_code(400));
}
?>
在UI上,我有以下简单且最少的代码:
And on UI i have this simple and minimal code:
var userLoginObject = {
email: 'login@email.com',
password: 'password123'
};
var authenticationResource = $resource('/api/authentication/authenticate.php');
function logIn() {
authenticationResource.save(userLoginObject );
}
这比使用丑陋的更好和更干净
That's much better and cleaner than using ugly
return $http({
method: 'POST',
url: '/api/drivers/authenticate.php',
data: $.param(userLoginObject),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
这篇关于如何在PHP脚本中使用angularjs $ resource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!