繁体   English   中英

AJAX不发回

[英]AJAX doesn't send back

首先,我想说我是Ajax的初学者。 我只是一个初学者。 我希望你能帮助我。

我想向ajax脚本发送ID,然后在其中运行查询。 然后,我想返回页面的输出。 我在其中设置了id并将其发送到ajax_project_info.php的页面中有一个quick_view_page.js页面,通过链接我在其中检索了ID。 如果我打开ajax_project_info.php并在链接中设置一个变量id,它会很好地工作。

我无法将ajax_project_info的输出返回给quick_view_page.js。

我的quick_view_page.js。

//Quick view show and hide
    $('a#quick_view').click(function() {
        var projectId = $(this).find($(".qv_id")).attr('id');
        //alert(projectId);

        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","ajax/ajax_project_info.php?projectId=" + projectId);
        xmlhttp.send();

        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementByClassName("qvp_bg").innerHTML=xmlhttp.responseText;
        }

我的ajax_project_info.php

<?php
require_once("../pages/db.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
ob_start();

$projectId = $_GET["projectId"];

$query_getinfo = "SELECT * FROM tblProjecten WHERE id = '".$projectId."'";

$result_query_getinfo = mysqli_query($conn,$query_getinfo);

while ($row = mysqli_fetch_array($result_query_getinfo)) {
    $response = $row['projectnaam'];
};
else {
    $response = "Geen info beschikbaar";
}
return $response;

?>

谢谢!

更新:问题似乎出在“如果(xmlhttp.readyState == 4 && xmlhttp.status == 200)”中,当我在两者之间设置警报时,该警报不会弹出。

更新2:我现在有quick_view_search.js:

        //Quick view show and hide
    $('a#quick_view').click(function() {
        var projectId = $(this).find($(".qv_id")).attr('id');


        //xmlhttp request
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementsByClassName("qvp_bg").innerHTML=xmlhttp.responseText;
            }
        }

        xmlhttp.open("GET","ajax/ajax_project_info.php?projectId=" + projectId, true);
        xmlhttp.send();

        $.ajax({
            'url' : 'ajax/ajax_project_info.php',
            'type' : 'GET',
            'data' : {
            'projectId' : projectId
        },
        'success' : function(data) {
            document.getElementsByClassName("qvp_bg").innerHTML=data;
        }
        });


        $('.quick_view_project').css('display', 'block').hide().fadeIn();
        return false;
    });

ajax_project_info.php:

<?php
/*
require_once("../pages/db.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
ob_start();

$projectId = $_GET["projectId"];

if ($projectId) {
    $query_getinfo = "SELECT * FROM tblProjecten WHERE id = '".$projectId."'";
    $result_query_getinfo = mysqli_query($conn,$query_getinfo);

    while ($row = mysqli_fetch_array($result_query_getinfo)) {
        $response = $row['projectnaam'];
    }
    else {
        $response = "Geen info beschikbaar";
    }
}
else {
    $response = "Geen info beschikbaar";
}
echo $response;
*/
echo "test";

?>

它给了我以下错误:“未定义xmlhttp”。

使用echo $response而不是return $response

也尝试

xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementByClassName("qvp_bg").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","ajax/ajax_project_info.php?projectId=" + projectId, true);
xmlhttp.send();

使用jQuery

将jQuery包含到项目中

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>

GET请求

$('.myButton').click(function() {
  $.ajax({
    'url' : 'ajax/ajax_project_info.php',
    'type' : 'GET',
    'data' : {
      'projectId' : projectId
    },
    'success' : function(data) {
      document.getElementByClassName("qvp_bg").innerHTML=data;
    }
  });
});

希望这可以帮助 :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM