【问题标题】:JQuery Ajax returns Source codeJQuery Ajax 返回源代码
【发布时间】:2017-05-27 12:31:20
【问题描述】:

我在我的网站上使用 JQuery Ajax。我已经在我们的测试服务器上对其进行了测试,它运行良好,但是当我尝试将它放在我们的生产服务器上时,Ajax 只会返回网站本身的源代码。

JS 文件:

$(document).ready(function () {
    $('#sshin').keyup(function () {
        var query = $(this).val();
        console.log(query);
        if (query != '') {
            $.ajax({
                url: "search.php",
                method: "POST",
                data: {
                    query: query
                },
                success: function (data) {

                    console.log(data);

                }
            });
        }
    });

    $(document).on('click', 'article', function () {
        var text = $('#sshin').val();
        text = text.substring(0, text.lastIndexOf(',') + 1);
        text += $(this).text();

        var uniq = text.match(/\b\w+\b/g).filter(function (el, idx, a) {
            return idx === a.lastIndexOf(el);
        });

        text = uniq.join(',');

        $('#sshin').val(text);
        $('#sshout').fadeOut();
    });
});

PHP 文件:

<?php
  if(isset($_POST["query"])){
     $query = $_POST["query"];
     return '<ul><li>1</li><li>2</li></ul>';
  }
?>

知道为什么它返回的东西与应有的不同吗?

【问题讨论】:

  • “网站本身的源代码”是什么意思?你能发布一个例子吗?
  • ajax 没有问题。查看search.php的实现
  • @Pekka웃 几乎是文件中的 html 代码
  • 您是否有可能忘记使用 PHP 文件解析器?

标签: javascript php jquery ajax


【解决方案1】:

这个方法曾经对我有用,希望对我有帮助。告诉我。

 this->output->set_content_type('application/json');
        return $this->output->set_output(json_encode('<ul><li>1</li><li>2</li></ul>'));

【讨论】:

  • @Damon 只需删除代码的return '&lt;ul&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ul&gt;';。然后粘贴它并检查 console.log(data) 显示的内容。
  • 删除了 this 并且它可以工作,但仍然返回源代码.. 也许它有助于我找到这个:https://myserver.ch/search.php?query=test,etc 当你在 chrome 中进入检查然后在网络上..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 2015-09-23
  • 1970-01-01
  • 2016-09-19
相关资源
最近更新 更多