gpt4 book ai didi

javascript - 数据未传递给 api 调用

转载 作者:行者123 更新时间:2023-12-02 14:12:08 25 4
gpt4 key购买 nike

我正在使用 AngularJs(1.5) 和 Codeigniter 休息服务器开发一个项目。我想知道为什么数据 idTag 没有传递给 php?让我向您展示我的代码并通过注释进一步解释。

我有这个工厂

factory.get = function(idLocation, idTag){
console.log(idLocation, idTag); //Console log outputs 1 6, so data is here

return $http({
method: 'GET',
url: $location.protocol() + '://' + $location.host() + '/rest/api/locationtag/locationtag/' + idLocation,
data: {
idTag: idTag
}
}).then(function successCallback(response){
console.log(response);
// response is empty, because data isn't sent to PHP
return response.data;
},function errorCallback(response){
console.log('Error getting slide shows: ' + response.data.message);
});
};

这是我尝试获取代码的 PHP

public function locationtag_get($idLocation)
{
$condition['id_location'] = $idLocation;
$condition['id_tag'] = $this->get('idTag');
var_dump($condition);
die();
$locationTag = $this->locations_tags->get($condition);
if ($locationTag) {
$this->response($locationTag, REST_Controller::HTTP_OK);
}
}

var_dump 的输出是

 'id_location' '1'
'id_tag' null

那么问题来了,为什么或者如何正确地将数据从 GET 方法发送到 Codeigniter 中的 PHP Rest Controller ?

如果您需要任何其他信息,请告诉我,我会提供。预先感谢您!

最佳答案

对于 GET 请求,您应该使用 params 而不是 data(用于 POST 请求)。您可以在 AngularJS documentation 中确认这一点.

params – {Object.} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

当然,没有什么可以阻止您将它们添加到 URL,但您必须正确处理信息编码,而不仅仅是传递 JSON 对象。

关于javascript - 数据未传递给 api 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39453373/

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