1

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

这一切都有效,但不是 100% 确定为什么?我有下面的脚本,说明它的结构。我想知道的是 success: function(result) 是如何工作的。在我的 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的echo有什么关系?

4

3 回答 3

0

it is this:

server-side(php) echoes and ajax listens this in success function

simple:

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

you can pass anything you want.. array, list, dict, json...

here you can read with real examples what happens in the background when you work with ajax https://web.archive.org/web/20140203201657/http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

于 2012-11-09T11:58:52.557 回答
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
    });
}
于 2012-11-09T12:03:47.183 回答
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"
 }
});
于 2012-11-09T12:04:19.303 回答