ajax回调,如何传递消息和数据?
嗨,我正在学习一点ajax,有点困惑 这一切都有效,但不是100%确定为什么?我有下面的脚本,它是如何构造的。我想知道的是success:function(result)是如何工作的。在我的php中,我有1或0的回音,但我希望在我的php ie中包含更多信息和正确的消息 我的javascript是ajax回调,如何传递消息和数据?,ajax,codeigniter,Ajax,Codeigniter,嗨,我正在学习一点ajax,有点困惑 这一切都有效,但不是100%确定为什么?我有下面的脚本,它是如何构造的。我想知道的是success:function(result)是如何工作的。在我的php中,我有1或0的回音,但我希望在我的php ie中包含更多信息和正确的消息 我的javascript是 $("#username1").blur(function(){ var username1 = $('#username1').val(); $.ajax({ type: "POST
$("#username1").blur(function(){
var username1 = $('#username1').val();
$.ajax({
type: "POST",
url: "http://localhost/gonnabook/check_username_availability",
data: 'username1='+ username1,
success: function(result){
if(result == 1){
alert("true");
}
else{
alert("false");
}
}
});
php是
function check_username_availability(){
$check_this = $this->input->post('username1');
$data = $this->tank_auth->is_username_available($check_this);
// the above code does all the query and results in the data
if($data){
echo 1;
//ideally I would like to add a message here ie This is not available
}else{
echo 0;
}
}
到目前为止,我的理解是php中的echo 1是javascript中的success函数接收的内容。但是如何在php中包含消息呢?一个人可以通过数组或其他什么来完成,然后结果就是result.check或类似的东西吗?
要点是javascript中的结果与php的echo之间的相关性是什么 是这样的:
服务器端(php)响应,ajax在success函数中监听此消息
简单:
in php: echo "1" ==> in ajax: success will get this 1 as result argument
你可以传递你想要的任何东西。。数组、列表、dict、json
在这里,您可以通过真实的示例了解使用ajax时在后台发生的情况
你最好的选择是,而不是回显
0
或1
将是回显JSON对象。。。试试这个
PHP
function check_username_availability(){
//This array will hold the data we return to JS- as many items as you want
$result=array();
$check_this = $this->input->post('username1');
$data = $this->tank_auth->is_username_available($check_this);
// the above code does all the query and results in the data
if($data){
$result['success']=1;
$result['message']='This is a random message';
$result['data']=data; //You can even return the retrieved data
}else{
$result['success']=0;
}
die(json_encode($result)); //Encode the array into JSON and echo the string
}
JS
$("#username1").blur(function(){
var username1 = $('#username1').val();
$.post(
"http://localhost/gonnabook/check_username_availability",
{'username1': username1},
function(result){
if (result.success==1){
alert('success: ' + result.message);
} else {
alert('error');
}
},
'json' //Tell JS we expect JSON in return
});
}
在PHP脚本中通常做的是将数据序列化为JSON字符串返回。然后,可以在ajax调用的成功回调中使用该对象 php脚本:
function check_username_availability(){
//do something fancy
//create your JSON return
var $data = array("foo" => "bar","bar" => "foo");
echo json_encode($data);
}
$.ajax({
type: "POST",
url: "http://localhost/gonnabook/check_username_availability",
data: 'username1='+ username1,
success: function(data){
console.log(data['foo'] + ' ' + data['bar']); //echos "bar foo"
}
});
客户端:
function check_username_availability(){
//do something fancy
//create your JSON return
var $data = array("foo" => "bar","bar" => "foo");
echo json_encode($data);
}
$.ajax({
type: "POST",
url: "http://localhost/gonnabook/check_username_availability",
data: 'username1='+ username1,
success: function(data){
console.log(data['foo'] + ' ' + data['bar']); //echos "bar foo"
}
});
谢谢你的帮助,现在完全有道理了。现在效果更好了。非常感谢,太好了。快乐,我的朋友!谢谢你的帮助:)星期五我学到了一些新东西。谢谢你的建议。我很感激,这很有帮助。祝你周末愉快。