0

我正在尝试立即对输入更改的关键字进行数据库查询。现在,我得到了一个成功的查询,所有结果都存储了,但是在GET显示它们时,我的ajax函数返回 false。为什么这样做?

控制台输出:

POST http://example.com/functions/ajax.php

response: the data i need to display

其次是

GET http://example.com/functions/ajax.php

response: false

这是我的 JS:

<script id="source" language="javascript" type="text/javascript">
$(function () {
    $('#url').bind('input', function () {
        $(this).val() // get  value
        $.ajax({
            type: 'POST',
            url: 'functions/ajax.php',
            data: {
                url: $('#url').val()
            },
            success: function (data) //on receive of reply
            {
                $(function () {

                    $.ajax({
                        url: 'functions/ajax.php', //the script to call to get data          
                        data: "", //you can insert url arguments here to pass to api.php
                        //for example "id=5&parent=6"
                        dataType: 'json', //data format      
                        success: function (data) //on receive of reply
                        {
                            var namePHP = data[1];
                            var categoryPHP = data[2];

                            //--------------------------------------------------------------------
                            // 3) Update html content
                            //--------------------------------------------------------------------

                            $('#name').html(namePHP); // # name and #category are input fields I want autofilled
                            $('#category').html(categoryPHP);


                        }
                    });
                });
            }
        });
    });
});
</script>

ajax.php

<?php
require_once ('DBconnect.php');

$url = $_POST['url'];

$url = mysqli_real_escape_string($con, $url);

$query = "SELECT * FROM `inserted_posts` WHERE `search_name` = '$url'";
$result = mysql_query($query);

$array = mysql_fetch_array($result);

echo json_encode($array);
?>
4

2 回答 2

1

您的ajax.php文件依赖于$_POST变量。这意味着您必须使用该POST方法发送数据。否则,PHP 就是看不到它。

于 2013-04-24T18:17:43.017 回答
0

将 $url 的变量分配更改为从 $_REQUEST 而不是 $_POST

$url = $_POST['url'];

$url = $_REQUEST['url'];
于 2013-04-24T18:18:11.873 回答