简体   繁体   中英

how do i get a response from a jquery ajax request

I am working on my site and i have a jquery request to the server

$.ajax(


        // do an ajax to send value to the database...
                        {
                            url:"pages/welcome_get.php",
                            type: "POST",
                            dataType: "json",
                            cache: false,
                            data: { wm_val: wel}

                        })

How can I get a response as a json data from which is not a html data and how do I parse the json response from the server to the html file?

You write the PHP to emit JSON.

<?php
# Some code to populate $some_associative_or_non_associative_array

header("Content-Type: application/json");
echo json_encode($some_associative_or_non_associative_array);
?>
You need to use the parseJSON function in the js.

Here is the Php code:

function send_reply(){
   echo json_encode(array('reply'=>'here is my reply'));
   exit;
}

Here is the js code:

$.ajax({
        url:'myajax.php',
        data:{'func':send_reply},
        type:'POST',
        dateType:'json'
    }).success(function(data){
                  data=$.parseJSON(data);
        alert(data.reply);
    }).error(function(jqxhr,error,status){
                      alert('Error sending reply');

    });

As @Quentin said, you need to output JSON in your PHP result. Then you need to use done() to receive the data on the client side and work with it from there:

$.ajax({
    url:"pages/welcome_get.php",
    type: "POST",
    dataType: "json",
    cache: false,
    data: { wm_val: wel}
}).done(function( json ) {
    // do something with json here
}); 

You need to add the "JSON" header to your "pages/welcome_get.php" file:

header("Content-Type: application/json");

And also in your AJAX call remember to add the "success" and "error" callback:

jQuery.ajax({
            url:"pages/welcome_get.php",
            type: "POST",
            dataType: "json",
            cache: false,
            data: { wm_val: wel}
            success: function(response) {
               //Do stuff with response here    
            }
            error: function(){
               //Display error msg or something like that
            }
        });

lets say you are checking user in your welcome_get.php

then in your welcome_get.php

use this

if(isset($_GET['wm_val'])){
$wm_val = $mysqli->real_escape_string($_GET['wm_val']);
$check_user = $mysqli->prepare("SELECT email FROM members WHERE username = ? LIMIT 1 ");
$check_user->bind_param('s', $wm_val);
$check_user->execute();
$check_user->store_result();
$check_user->bind_result( $email);
$check_user->fetch() ; 

if ($check_user->num_rows == 1) { $datas['msg']= "failed" ;}
else{$datas['msg']= "success" ;}
$check_user->close() ;
 echo json_encode($datas);   
}

and your ajax

  $.ajax(


    // do an ajax to send value to the database...
                    {
                        url:"pages/welcome_get.php",
                        type: "POST",
                        dataType: "json",
                        cache: false,
                        data: { wm_val: wel},
                        success: function(msg) {
                          if (data.msg == 'success'){
                             //  do what you like here example
                              $('#mydata').html("<span >you welcome ! </span>").delay(4000).fadeOut('fast');
                         }else{
                             //do something else example 
                            $('#mydata').html("<span >failed ! </span>").delay(4000).fadeOut('fast');
                           }

                    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM