gpt4 book ai didi

javascript - AngularJS $http 服务成功但数据库未更新

转载 作者:可可西里 更新时间:2023-11-01 07:39:14 25 4
gpt4 key购买 nike

我是 AngularJS 的新手,我正在尝试执行 http 服务以将数据发送到 php 文件以输入到 MySQL 中。我已经能够使用 jQuery 和 $.ajax 函数让它工作,所以我确信我的 PHP 没问题。

我的最终问题:我实现此 AngularJS 服务的方式是否正确?我已将 ng-click 绑定(bind)到 a 元素上名为 routeReloader(data) 的函数。当我 console.log routeReloader 函数中的参数时,我得到了正确的参数。我还收到“成功”的警告框。但是,它不会更新 MySQL。

userApp.controller('photoController',  ['$scope','$location','$log','$route','$http',    function($scope,$location,$log,$route,$http){

$scope.routeReloader = function(parameters){
$http.post('centralcommand.php', {id: parameters}).success(function(data,status,headers,config){
alert('success');
}).error(function(data,status,headers,config){
alert('failure');
});
};

}]);

我使用 $_POST['id'] 获取 centralcommand.php 文件中的数据。

JQuery 工作等价物

$('#link-element').click(function(){

var REL = $(this).attr("rel");
var URL = 'centralcommand.php';
var dataString = 'id=' + REL;

$.ajax({
type: "POST",
url: URL,
data: dataString,
cache: false,
success : function() {
alert('success')
}
});
});

最佳答案

我能够根据评论找到解决方案,因为所传递的数据是 JSON 格式的。我必须将 JSON 格式解码为 PHP 可以使用的格式:

PHP 代码

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$photoID = $request->id;

使用此 PHP 代码转换 JSON 格式,您可以在 SQL 查询中使用它。请注意,$request->id 使用在 AngularJS $http post 请求中传递的相同 id

关于javascript - AngularJS $http 服务成功但数据库未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com