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

时间:2014-03-14 15:04:45

标签: php jquery html mysql ajax

我在我的网站上工作,我有一个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}

                        })

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

5 个答案:

答案 0 :(得分:2)

您编写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);
?>

答案 1 :(得分:0)

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

    });

答案 2 :(得分:0)

正如@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
}); 

答案 3 :(得分:0)

您需要添加&#34; JSON&#34;标题为您的&#34;页面/ welcome_get.php&#34;文件:

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

并且在您的AJAX通话中记得添加&#34;成功&#34;和&#34;错误&#34;回调:

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

答案 4 :(得分:0)

假设您正在检查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');
                           }

                    })