繁体   English   中英

ajax成功函数未返回json解码数据

[英]ajax success function not returning json decoded data

我已经在stackoverflow上搜索了类似的问题,但没有任何帮助。 这是我对ajax的adding.php文件的调用。 我在jquery keyup事件上称它为。 当我在浏览器中检查时,我看到php文件返回响应。 但是,响应的数据永远不会达到ajax的成功功能。

$.ajax({
    url: "anding.php",
    type: "POST",
    dataType: 'json',
    data: JSON.stringify({mycol:mycol,mycolval:mycolval,string:string}),
    contentType: 'application/json',
    success: function(data){
        alert(data);
        var output = data.substring(0, data.indexOf('arventures'));
                        last = data.substring(data.indexOf('arventures') + 10);
                        last--;
                        $('.remove').remove();
                        $('.main_tr').after(output);

                        if (output == '' || output == null) {
                            $('.message').html('No results found.');
                        }
                        else {
                            $('#row1').addClass('highlight');
                        }//highlight row1 by default

                            }

   });

这是我的PHP文件,它返回响应。 我粘贴了整个代码,因为我不知道是哪个部分导致了问题。 php.php

<?php
include 'connection.php';
$postdata = json_decode(file_get_contents("php://input"), true);

//var_dump($postdata);exit;
//var_dump($postdata);
$query = "SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '".$table_name."'
    AND table_schema = '".$mysql_database."'";
$result = mysqli_query($con,$query);


$results = array();
while ($line = mysqli_fetch_assoc($result)) {
    $results[] = $line;
}



$query = null;


foreach ($results as $r) {//search in order.1st search in column 1 then column 2...so on
    $append = " SELECT * from " . $table_name . " WHERE " . $r['COLUMN_NAME'] . " like '" . $postdata['string'] . "%'";
    $query = $query . $append;
    for($i=1;$i<=(count($postdata['mycol']))-1;$i++)
        {
    $append1=" AND " .$postdata['mycol'][$i]. " like '" . $postdata['mycolval'][$i] . "%'";
    $query = $query . $append1;
        }
        $query=$query." UNION";
}
$query = substr($query, 0, -6);



$result2 = mysqli_query($con, $query);
$pos = strrpos($postdata['string'], '%');
$str_count = substr_count($postdata['string'], '%');
$results2 = array();
$results3 =array();
while ($line = mysqli_fetch_assoc($result2)) {
    if (strpos($postdata['string'], '%') !== false || strpos($postdata['string'], '_') !== false) {// highlight in star search
        $str = preg_replace('/[^a-zA-Z0-9-]/', '', $postdata['string']);
        $line = preg_replace("|^($str)|Ui", "<span class='highlights'>$1</span>", $line);
    } else {
        $string=$postdata['string'];
        $line = preg_replace("|^($string)|Ui", "<span class='highlights'>$1</span>", $line); //highlight in normal search 
    }
    $results2[] = $line;
}

$result2 -> data_seek(0);

while ($line1 = mysqli_fetch_assoc($result2)) {

    $results3[] = $line1;
}


for ($i=1;$i<=count($results2);$i++) {

   echo "<tr id='row".$i."' class='remove table_row'>";
   $j=0;
        foreach($results as $r1){
            if($j==0){
                echo "<td class='index_field' dB_id='".$results3[$i-1][$r1['COLUMN_NAME']]."'>".$results2[$i-1][$r1['COLUMN_NAME']]."</td>";
            } else {
                echo "<td>".$results2[$i-1][$r1['COLUMN_NAME']]."</td>";
            }
            $j++;
        }
   echo "</tr>";
}
echo 'arventures' . $i;

mysqli_close($con);

您的ajax调用永远不会到达成功函数,因为您已将dataType指定为JSON。 删除dataType或返回JSON而不是常规HTML。

暂无
暂无

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

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