ajax回拨,如何传递消息和数据?

时间:2012-11-09 11:54:34

标签: ajax codeigniter

嗨,我正在学习一点ajax,有点困惑。

一切正常但不是100%肯定为什么?我有下面的脚本如何结构化。我想知道的是成功:功能(结果)如何运作。在我的PHP我有1或0的回声但我想在我的PHP中包含更多信息,即正确的消息。

我的javascript是。

$("#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中的成功函数所接收的。但是如何在php中包含一条消息?可以通过数组或其他东西来做,然后结果是result.check或类似的东西? 重点是javascript和php的回声结果的相关性是什么?

3 个答案:

答案 0 :(得分:0)

就是这样:

服务器端(php)回声和ajax在成功函数中监听它

简单:

in php: echo "1"  ==> in ajax: success will get this 1 as result argument

你可以传递你想要的任何东西.. array,list,dict,json ......

在这里,您可以阅读实际示例,当您使用ajax时,后台会发生什么 https://web.archive.org/web/20140203201657/http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

答案 1 :(得分:0)

您最好的选择是,而不是回显01,而不是回应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
    });
}

答案 2 :(得分:0)

您通常在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"
 }
});
相关问题