0

What am I doing wrong?

For some reason the success function's html.responseText is a verbatim copy of the domain's index.php. This call isn't even made in index.php, nor does it address it—what's going on?

The Ajax:

var k = '123abc';
$.ajax({
    URL: 'php/dbInsertKey.php',
    type: 'POST',
    dataType: 'JSON',
    data: {
        key: k
    },
    success: function(html){
        console.log(html);
    }
});

dbInsertKey.php:

$key = (string) $_POST['key'];
echo ($key);

instead of returning '123abc', it returns a string that contains the domain's index.php, line for line. And for what it's worth, this happens no matter what the supplied URL is: 'php/nonsense.lol' will give the same result.

VHK
  • 89
  • 5
  • if the url is wrong, and index is the defult 404 page ... thats what happens –  Feb 26 '17 at 01:29
  • I've tried every url there is. This file and dbInsertKey.php are in the same folder. if I take away the 'php/' it will still give me the index as responseText – VHK Feb 26 '17 at 01:33
  • perhaps you could tell us a little about the "server environment" ... apache? nginx? something else? – Jaromanda X Feb 26 '17 at 01:34
  • Apache. Other files make and receive ajax calls perfectly as we speak. It's just this one. – VHK Feb 26 '17 at 01:36

2 Answers2

2

Answer (updated)

You are using uppercase URL in your call to jquery - it should be lowercase url . I still don't see how this would lead to you seeing your php file, but it seems like it may be somehow a factor

My Original Answer (still relevant for others finding this in a similar situation)

Sounds like your webserver may not be configured correctly for PHP (or you don't even have PHP installed)

See here for more help diagnosing/fixing: Apache shows php code instead of executing

Community
  • 1
  • 1
Theo
  • 1,608
  • 1
  • 9
  • 16
  • It does sound like that, but I've been using it for years, doing exactly this type of stuff. – VHK Feb 26 '17 at 01:34
  • I would still investigate, Assume nothing, rule out the obvious first - You mention that other files work, but at the same time this happens for any URL - those statements seem at odds with each other, could you explain a little more – Theo Feb 26 '17 at 01:39
  • index.php contains a menu, and a div in which content is loaded via ajax calls from other php's. Think "settings.php" being loaded into `$('#menu_window')` when a button is clicked. Settings.php in turn contains other ajax calls that insert or retreieve stuff from the database. This has worked fine, and is working fine as we speak. But this particular file won't for the love of god receive as much as a `echo ('test')` from the called upon dbInsertKey.php. – VHK Feb 26 '17 at 01:40
  • You are using uppercase `URL` in your call to jquery - it should be lowercase [`url`](http://api.jquery.com/jquery.ajax/) . I still don't see how this would lead to you seeing your php file, but it seems like it may be somehow a factor – Theo Feb 26 '17 at 01:45
  • @VHK no worries, it's easy to miss these things, when symptoms point you to a more obvious place to look. I have updated my answer - I would be interested to know if you ever figure out why it resulted in you seeing your PHP though. That sounds slightly worrying from a security perspective. – Theo Feb 26 '17 at 02:00
0

The URL argument should be in lowercase. Thanks, Theo, for noticing that.

VHK
  • 89
  • 5