CodeIgniter 4-从数据库问题中获取结果
我是Codeigniter 4的新手,并且有一些问题从数据库中检索数据。
在我的控制器中,我有:
$partno = model(Ecart_model::class)->getPartno($productid);
echo json_encode($partno);
在我的模型中,我有:
public function getPartno($productid){
$db = db_connect();
$partno = $db->table('products')->select('partno')->where('id', $productid)->get();
return $partno;
}
在我的ajax函数中,我有:
function addtoecart(ip,productid){
var postData = {ip: ip, productid: productid};
$.ajax(
{
url : 'https://www.myurl.com/ecart/add',
type: 'POST',
data : postData,
dataType: "json",
success: function(result) {
alert(result);
},
});
}
功能性方面的一切(这不是问题)。我存在的问题是从数据库中检索到的值/数据,并将其传递回AJAX函数,在成功之后,警报显示:[对象对象],而不是从数据库中检索到的实际值,
我希望这很有意义?您知道代码怎么了吗?
谢谢!
I'm new to codeigniter 4 and having some issues retrieving data from my database.
In my controller, I have:
$partno = model(Ecart_model::class)->getPartno($productid);
echo json_encode($partno);
In my model, I have:
public function getPartno($productid){
$db = db_connect();
$partno = $db->table('products')->select('partno')->where('id', $productid)->get();
return $partno;
}
In my Ajax function, I have:
function addtoecart(ip,productid){
var postData = {ip: ip, productid: productid};
$.ajax(
{
url : 'https://www.myurl.com/ecart/add',
type: 'POST',
data : postData,
dataType: "json",
success: function(result) {
alert(result);
},
});
}
Everything in terms of functionality works (which isn't the issue). The issue I am having is the value/data that is retrieved from the database and is passed back to the Ajax function, on success, the alert shows: [object Object] instead of the actual value retrieved from the database
I hope that makes sense? Do you know what is wrong with the code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是答案吗?
我也不确定jQuery在AJAX结果上解码JSON数据,因此我也会尝试
json.parse(result)。尽管当您返回
int
时,您可能根本不需要JSON,并且可以简单地输出它。Is this not the answer?
I'm also not sure that Jquery decodes json data on an ajax result so I would also try
JSON.parse(result)
as well. Although as you're returning anint
you probably don't need the JSON at all and could simply output it.