0

I'm using JQuery Ajax on my website. I've tested it on our test server it worked perfectly, but when I tried to put it on our productive server the Ajax just returns the source code of the website itself.

JS File:

$(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 File:

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

Any idea why it returns something different than it should?

Damon
  • 127
  • 1
  • 2
  • 15
  • 1
    What do you mean by "source code of the website itself"? Can you post an example bit? – Pekka Jan 12 '17 at 11:53
  • 2
    Nothing wrong with `ajax`. see the implementation of `search.php` – Jyothi Babu Araja Jan 12 '17 at 11:53
  • Possibly related: http://stackoverflow.com/questions/7264014/why-is-my-php-source-code-showing – Pekka Jan 12 '17 at 11:53
  • @Pekka웃 it's litterally the html code from the file – Damon Jan 12 '17 at 11:54
  • Is it any possibility that you forget to use a parser of your PHP files? – Chu Ya'an Xiaonan Jan 12 '17 at 11:54
  • @JyothiBabuAraja it works fine on our test server but i didnt change a single bit of the code.. – Damon Jan 12 '17 at 11:55
  • Can you post the `search.php` and actual response you are getting now? – Jyothi Babu Araja Jan 12 '17 at 11:56
  • Maybe you receive error and see HTML output instead of PHP output? – Justinas Jan 12 '17 at 12:02
  • Do you have to enable html file extension parsing with Apache? You may want to compare settings between your environments. – Neo Jan 12 '17 at 12:04
  • @MisterPositive no difference between, already checked that.. could it be some way of redirect or something? – Damon Jan 12 '17 at 12:06
  • What is "the html code from the file"? You still haven't been precise. Is this the PHP code you mean, including the ` – moopet Jan 12 '17 at 12:07
  • @moopet just HTML, the html code the website is displaying including the Javascript, my css, etc..... – Damon Jan 12 '17 at 12:09
  • From what I can tell the server is configured to return soft 404 (returns the home page when a page is not found with 200 OK). In this case the ajax receives the whole HTML of redirected page. – Rishabh Jan 12 '17 at 12:19
  • @Rishabh that's actually something to work with, but still how is it possible that when i change the url to something wrong, that it returns a normal 404? – Damon Jan 12 '17 at 12:20

1 Answers1

0

This method once worked for me, hope it might help. Let me know.

 this->output->set_content_type('application/json');
        return $this->output->set_output(json_encode('<ul><li>1</li><li>2</li></ul>'));
XAF
  • 1,462
  • 1
  • 11
  • 22
  • @Damon just remove the `return '
    • 1
    • 2
    ';` of your code. and then paste this and check what console.log(data) shows.
    – XAF Jan 12 '17 at 13:07
  • removed `this` and it works, but still returns the source code.. maybe it helps i found this: `https://myserver.ch/search.php?query=test,etc` when you go into inspect in chrome and then on network.. – Damon Jan 12 '17 at 13:12