As the title, i want to CI_Controller return value/data back to AJAX which give request before.
作为标题,我想要CI_Controller将值/数据返回给AJAX,它会在之前发出请求。
The_Controller.php
The_Controller.php
class The_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
function postSomething()
{
$isHungry = true;
if(isHunry){
return true;
}else{
return false;
}
}
}
}
AJAX
AJAX
$(document).ready(function() {
$(文档)时函数(){
$('#form').on('submit', function (e)
{
e.preventDefault(); // prevent page reload
$.ajax ({
type : 'POST', // hide URL
url : '/index.php/The_Controller/postSomething',
data : $('#form').serialize (),
success : function (data)
{
if(data == true)
{
alert ('He is hungry');
}
else
{
alert ('He is not hungry');
}
}
, error: function(xhr, status, error)
{
alert(status+" "+error);
}
});
e.preventDefault();
return true;
});
});
The Problem :
存在的问题:
The AJAX code above always get FALSE for variable data. It's not get return value from postSomething function inside CI_Controller.
上面的AJAX代码对于变量数据总是为FALSE。它不会从CI_Controller中的postSomething函数获得返回值。
The Question :
一个问题:
I want to if(data == true) result alert ('He is hungry');. But cause data not fill by return value of postSomething function inside CI_Controller so it always false. How to get return value/data from CI_Controller to AJAX?
我想要if(data == true) result alert ('He is hungry');但是由于CI_Controller中的postSomething函数的返回值不填充数据,所以总是为false。如何从CI_Controller返回值/数据到AJAX?
Thank you
谢谢你!
2 个解决方案
#1
1
you need to change your code a little bit. no need to return data, just echo true
or echo false
from your controller. Hope you will get your desired result.
您需要稍微修改一下代码。不需要返回数据,只需从控制器中返回true或echo false。希望你能得到你想要的结果。
#2
1
You have a typo in the PHP code
PHP代码中有一个错码
if(isHunry){
should be
应该是
if($isHungry){
Also, returning data to an AJAX request, you really should be sending the correct content type in the header. Ex.
此外,将数据返回到AJAX请求时,实际上应该在头中发送正确的内容类型。前女友。
header('Content-Type: application/json');
And print
or echo
the data, not return
it, as well as json_encode
it:
并打印或回显数据,而不返回数据,以及json_encode:
echo json_encode($data);
So your postSomething
php function should look something like this:
所以你的postSomething php函数应该是这样的:
$isHungry = true;
header('Content-Type: application/json');
echo json_encode($isHungry);
#1
1
you need to change your code a little bit. no need to return data, just echo true
or echo false
from your controller. Hope you will get your desired result.
您需要稍微修改一下代码。不需要返回数据,只需从控制器中返回true或echo false。希望你能得到你想要的结果。
#2
1
You have a typo in the PHP code
PHP代码中有一个错码
if(isHunry){
should be
应该是
if($isHungry){
Also, returning data to an AJAX request, you really should be sending the correct content type in the header. Ex.
此外,将数据返回到AJAX请求时,实际上应该在头中发送正确的内容类型。前女友。
header('Content-Type: application/json');
And print
or echo
the data, not return
it, as well as json_encode
it:
并打印或回显数据,而不返回数据,以及json_encode:
echo json_encode($data);
So your postSomething
php function should look something like this:
所以你的postSomething php函数应该是这样的:
$isHungry = true;
header('Content-Type: application/json');
echo json_encode($isHungry);