JQuery AJAX请求答案但没有成功

时间:2012-12-19 05:58:55

标签: php jquery mysql ajax

我有这个文本框用于检查数据库上的名称,它提供答案和所有内容但是如果我放console.log(data)它没有回答。

这是我的代码:

            function absurdSearch(textoBuscar){
            $.ajax({ 
            type    : "POST",
            url     : "service.php",
            dataType: "json",
            data    : {
                action:"absurdSearch",
                absurdText: textoBuscar
                },          
                success:function(data){
                    console.log(data);
                    $("#fotoproyectosarq").empty();
                    var html = '';
                    html += '<img src="' + data.path + '" height="128" width="160">';
                    $("#fotoproyectosarq").append(html);

                    $("#nombreproyectosarq").empty();
                    var html2 = '';
                    html2 += "<form method=\"post\" name=\"projectsearch\" id=\"projectsearcharq\" action=\"proyectos_arq.php\">" 
                    html2 += "<span style=\"cursor: pointer;\" onclick=\"document.getElementById('projectsearcharq').submit()\">"+ data.projectName +"</span>"
                    //html2 += "<button id=" + "button" + data.projectId + " style=\"visibility:hidden;\"><span id=" + data.projectId + " style=\"cursor: pointer;\"><span>" + data.projectName + "</span></span></button>"
                    html2 += "<input name=\"project_id\" type=\"hidden\" id=\"project_id\" value=" + data.projectId + ">"   
                    html2 += "</form>"
                    $("#nombreproyectosarq").append(html2);


                }
            })
        }

    $('#buscadorRapidoTextInput').on('keyup', function() {
        var textoBuscar = $(this).attr('value');
        absurdSearch(textoBuscar);
    });

并且服务根据firebug回答:

memphis{"projectCategory":"1","projectId":"5","projectName":"MEMPHIS RIVERFRONT","path":"server\/php\/files\/MF_1.jpg"} 

但成功事件似乎不起作用。

这是后端的代码:

    function absurdSearch($absurdText){

    $db   = new db();
    $conn = $db->conn();

    $SQL_ABSURD_SEARCH="SELECT projects.id as projectId, projects.project_types_id as projectCategory, projects.name as projectName, projects.description as description, path  FROM projects INNER JOIN project_types ON ( projects.project_types_id = project_types.id) inner join images on images.projects_id = projects.id where images.main = '1' and projects.name LIKE '%$absurdText%' LIMIT 1";
    $conn->query($SQL_ABSURD_SEARCH);
            foreach($conn->query($SQL_ABSURD_SEARCH) as $row) {
                $projectCategory = $row['projectCategory'];
                $projectId = $row['projectId'];
                $projectName = $row['projectName'];
                $path = "server/php/files/".$row['path']."";

                $rows = array("projectCategory" => $projectCategory,"projectId" => $projectId, "projectName" => $projectName, "path" => $path);            

                $json = json_encode($rows);

                echo $json;
            }
}

1 个答案:

答案 0 :(得分:0)

你需要打电话

exit(json_encode($rows);

这应该可以解决您的问题