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

时间:2022-10-08 08:57:39

Hi I am learning a bit of ajax and a bit confused.

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

It all works but not 100% sure why? I have the script below of how its structured. What I want to know is how the success: function(result) works. In my php I have the echo for 1 or 0 but I would like to include more info in my php ie the correct message.

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

My javascript is.

我的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 is

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;

        }
 }

So far my understanding is the echo 1 in the php is what the success function in the javascript receives. But how would include a message in the php? Can one do it via an array or something and then the result is result.check or something like that? The main point is what is the correlation of the result in the javascript and echo of the php?

到目前为止,我的理解是php中的echo 1是javascript中的成功函数所接收的。但如何在PHP中包含一条消息?可以通过数组或其他东西来做,然后结果是result.check或类似的东西?重点是javascript和php的回声结果的相关性是什么?

3 个解决方案

#1


0  

Your best bet is to, instead of echoing 0 or 1 would be to echo a JSON object... Try this...

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

#2


0  

What you usually do in your PHP script is to return your data serialized as a JSON string. This object then can be used in the success callback of the ajax call

您通常在PHP脚本中执行的操作是将序列化的数据作为JSON字符串返回。然后,此对象可用于ajax调用的成功回调

php script:

function check_username_availability(){

 //do something fancy

 //create your JSON return
 var $data = array("foo" => "bar","bar" => "foo");
 echo json_encode($data);
}

client:

$.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"
 }
});

#3


0  

it is this:

它是这个:

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

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

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...

你可以传递你想要的任何东西..数组,列表,字典,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/

在这里,您可以阅读实际示例,当您使用ajax时,后台会发生什么情况https://web.archive.org/web/20140203201657/http://net.tutsplus.com/tutorials/javascript-ajax/5-ways -to-使Ajax的通话与 - jQuery的/

#1


0  

Your best bet is to, instead of echoing 0 or 1 would be to echo a JSON object... Try this...

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

#2


0  

What you usually do in your PHP script is to return your data serialized as a JSON string. This object then can be used in the success callback of the ajax call

您通常在PHP脚本中执行的操作是将序列化的数据作为JSON字符串返回。然后,此对象可用于ajax调用的成功回调

php script:

function check_username_availability(){

 //do something fancy

 //create your JSON return
 var $data = array("foo" => "bar","bar" => "foo");
 echo json_encode($data);
}

client:

$.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"
 }
});

#3


0  

it is this:

它是这个:

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

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

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...

你可以传递你想要的任何东西..数组,列表,字典,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/

在这里,您可以阅读实际示例,当您使用ajax时,后台会发生什么情况https://web.archive.org/web/20140203201657/http://net.tutsplus.com/tutorials/javascript-ajax/5-ways -to-使Ajax的通话与 - jQuery的/