php web服务无法读取我的键值
我使用的是angularjs 1.6.4版本,我将我的密钥和值发送到php web服务,但我的密钥和值未读取,我在存储在“0”的数据库中获得结果,这是我的angularjs代码php web服务无法读取我的键值,php,angularjs,web-services,api,Php,Angularjs,Web Services,Api,我使用的是angularjs 1.6.4版本,我将我的密钥和值发送到php web服务,但我的密钥和值未读取,我在存储在“0”的数据库中获得结果,这是我的angularjs代码 $scope.fav = { "userid":101, "favid":120 } $http({ method:"POST", url:apiurl+"addFavorite
$scope.fav = {
"userid":101,
"favid":120
}
$http({
method:"POST",
url:apiurl+"addFavorites.php",
data:JSON.stringify($scope.fav)
}).then(function(data)
{
$scope.favorites = data.data;
alert(data.data.message);
});
这是我的php rest api
include("../includes/db.php");
//creating response array
$response = array();
$request_method = $_SERVER['REQUEST_METHOD'];
if ($request_method == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') {
$request_method = 'DELETE';
} else if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
$request_method = 'PUT';
} else {
throw new Exception("Unexpected Header");
}
}
if($request_method == "POST"){
//getting values
$userid = isset($_POST['userid']) && $_POST['userid'] != '' ? trim($_POST['userid']) : "";
$favid = isset($_POST['favid']) && $_POST['favid'] != '' ? trim($_POST['favid']) : "";
if ($favid == 0) {
$strupdate = mysql_query("insert into nr_favourites(UserProfileId,FavouriteUserProfileId,CreatedDate)Values('$userid','$favid',now())");
}
if ($favid != 0) {
$sql = mysql_query("select * from nr_favourites where id=$favid");
$rc = mysql_num_rows($sql);
if ($rc != 0) {
$strupdate = mysql_query("insert into nr_favourites(UserProfileId,FavouriteUserProfileId,CreatedDate)Values('$userid','$favid',now())");
}
}
if ($strupdate)
{
$response['error']=false;
$response['message']='add favourites successfully!';
}
else
{
$response['error']=true;
$response['message']='add favourites not successfully.';
}
} else {
$response['error']=true;
$response['message']='You are not authorized';
}
header('Content-Type: application/json');
echo json_encode($response);
这些是我的代码,请帮助我解决此错误看起来您可能正在将字符串保存到数字字段。调用
JSON.stringify($scope.fav)
时,数字将转换为字符串
这里
$userid=isset($\u POST['userid'])和&$\u POST['userid']!=''?修剪($_POST['userid']):“”;
$favid=isset($_POST['favid'])和&$_POST['favid']!=''?修剪($_POST['favid']):“”代码>
由于user_id和favid是字符串,因此每次都将其设置为空字符串。我猜两者都是
nr_favourites.UserProfileId
nr_favourites.FavouriteUserProfileId
是接收字符串的数字字段,因此为0值。删除JSON.stringify()并保存空字符串而不是空字符串,这应该可以解决问题。您的问题与AngularJS相关,而不是AngularJS。请允许我编辑你的帖子。