繁体   English   中英

我如何从jquery ajax请求中获取响应

[英]how do i get a response from a jquery ajax request

我正在我的网站上工作,并向服务器发送了jQuery请求

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

                        })

如何获取不是html数据的json数据作为响应,以及如何将json响应从服务器解析为html文件?

您编写PHP以发出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');

    });

正如@Quentin所说,您需要在PHP结果中输出JSON。 然后,您需要使用done()在客户端接收数据并从那里进行处理:

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

您需要将“ JSON”标头添加到“ pages / welcome_get.php”文件中:

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

还要在AJAX调用中记住添加“成功”和“错误”回调:

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
            }
        });

可以说您正在用welcome_get.php检查用户

然后在您的welcome_get.php中

用这个

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);   
}

和你的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');
                           }

                    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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