I am using ajax to pass my variable into a controller. I can see the result in the success function data but when I want to echo the posted variable, I don't see any value.
我正在使用ajax将我的变量传递给控制器。我可以在成功函数数据中看到结果,但是当我想回显发布的变量时,我看不到任何值。
This is my ajax :
这是我的ajax:
id = $(this).attr("id");
$.ajax({
type:'POST',
url:'mobishopp/page',
data: {
'id': id
},
success:function(data) {
alert(data);
}
});
This is my controller:
这是我的控制器:
public function page()
{
$id= $this->input->post('id');
echo $id;
}
2 个解决方案
#1
1
In the controller when you echo $id
you are not sending output to the browser window. Instead you are sending output to the success
function of the ajax call. If you want it to display in the browser window you must do so in the success
function.
在回显$ id的控制器中,您没有将输出发送到浏览器窗口。而是将输出发送到ajax调用的success函数。如果要在浏览器窗口中显示它,则必须在成功功能中执行此操作。
#2
1
To see the result of ajax request and what it sent and what is received go to developer tools in google chrome in network tab select XHR then trigger the event that generate Ajax Request the request details will be shown in result such as data been send and status of the request and the response and error if there is an error
要查看ajax请求的结果及其发送内容和接收内容,请转到Google Chrome网络选项卡中的开发人员工具选择XHR然后触发生成Ajax请求的事件请求详细信息将显示在结果中,例如数据已发送和状态如果有错误,请求以及响应和错误
note in php to prevent execution after echo do the following
注意在php中阻止执行echo之后执行以下操作
echo $id;die();
I hope my answer would be useful
我希望我的回答很有用
#1
1
In the controller when you echo $id
you are not sending output to the browser window. Instead you are sending output to the success
function of the ajax call. If you want it to display in the browser window you must do so in the success
function.
在回显$ id的控制器中,您没有将输出发送到浏览器窗口。而是将输出发送到ajax调用的success函数。如果要在浏览器窗口中显示它,则必须在成功功能中执行此操作。
#2
1
To see the result of ajax request and what it sent and what is received go to developer tools in google chrome in network tab select XHR then trigger the event that generate Ajax Request the request details will be shown in result such as data been send and status of the request and the response and error if there is an error
要查看ajax请求的结果及其发送内容和接收内容,请转到Google Chrome网络选项卡中的开发人员工具选择XHR然后触发生成Ajax请求的事件请求详细信息将显示在结果中,例如数据已发送和状态如果有错误,请求以及响应和错误
note in php to prevent execution after echo do the following
注意在php中阻止执行echo之后执行以下操作
echo $id;die();
I hope my answer would be useful
我希望我的回答很有用