duankuiyuant3940 2015-06-15 15:44
浏览 136

解析从html到ajax的返回数据

I have some problem with the returned value of ajax.

this is the ajax code:

$(document).ready(function() {
    var request;
    $("#flog").submit(function(event) {
        if(request)
            request.abort();
        event.preventDefault();
        var form = $(this);
        var serializedData = form.serialize();
        var btnname = $('#log').attr('name');
        var btnval = $('#log').val();
        var btn = '&'+btnname+'='+btnval;
        serializedData += btn;      

        request = $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            data: serializedData,
        });

        request.done(function(data, status, jdXHR) {
            alert(data);
        });

        request.fail(function(jdXHR, status, error) {

        });
    });
});

it takes data from a form and send it to another page.

this is the second page:

<?php include 'head.php'; ?>
    <?php
    if($_POST['login']) {

    session_regenerate_id(true);
    $con = mysqli_connect("localhost", "Alessandro", "ciao", "freetime")
        or die('Could not connect: ' . mysqli_error($con));

    $query = 'SELECT * FROM users WHERE username="' . $_POST['user'] . '"';
    $result = mysqli_query($con, $query) or die('Query failed: ' . mysqli_error($con));
    if(mysqli_num_rows($result) == 0) {
        mysqli_close($con);
        session_unset();
        session_destroy();
        $res = false;
        return $res;
    }

    $query = 'SELECT password FROM users WHERE username="' . $_POST['user'] . '"';
    $result = mysqli_query($con, $query) or die('Query failed: ' . mysqli_error($con));
    $line = mysqli_fetch_array($result, MYSQL_ASSOC);
    if(md5($_POST['password']) != $line['password']) {
        mysqli_close($con);
        session_unset();
        session_destroy();
        return false;
    }
?>
<?php include 'foot.php'; ?>

and in .done the returned data is all the html page. How can I retrieve only a data, like $res? I tried with json_encode() but with no results. If in the second page I delete the lines include 'head.php' and include 'foot.php' it works. But I need that the secon page is html, too. Somenone can help me?

  • 写回答

2条回答 默认 最新

  • dongqiuge5435 2015-06-15 15:54
    关注

    Dont use the Data attribute from AJAX.

    Replace

    request.done(function(data, status, jdXHR) {
            alert(data);
        });
    

    with

    request.done(function(data, status, jdXHR) {
            alert(jdXHR.responseText);
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog