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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算