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 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)