ajax响应(JSON)没有正确调用函数

时间:2010-05-17 17:54:34

标签: jquery ajax json

由于某种原因,未在以下JavaScript代码中调用success函数:

$.ajax({  
            type: 'POST',
            url: 'http://localhost/hf_latest_desktop/st_pages/user_area/acc_buttons/pass_change/pass_change_ajax.php',
            data: data,
            dataType: 'json',
            success: function(e){ 
                console.log(e);
                if(e.status == 'success'){
                    alert('your password has been changed!');
                }   
                if(e.status == 'error1'){
                    alert('please fill in all inputs!');
                }
                if(e.status == 'error2'){
                    alert('password incorrect!');
                }
                if(e.status == 'error3'){
                    alert('change failed!');
                } 
            } 
        }); 

php文件ajax正在调用:

    <?php session_start();
    session_cache_limiter('nocache');
    header('Expires: ' . gmdate('r', 0));
    header('Content-type: application/json');
    $status = 'error1'; //for sake of example
    ?>
    {'status':'<?php echo $status; ?>'} 

1 个答案:

答案 0 :(得分:1)

添加错误处理程序以查看返回的内容。

$.ajax({
   type: 'POST',
   dataType: 'text/json',
   url: 'myUrl',
   success: function(data, status) { },
   error: function(data, status)
   {
      alert(data); // Print the response!
   }
});