0

Thanks for taking your time to help me out.

The situation:

I'm having some trouble getting some data by AJAX using jQuery on Cloud9 (http://c9.io). All files are in the same directory.

Here are the codes:

PHP (connection.php):

<?PHP
    echo "haha";
?>

Javascript:

var testAjax = function(){
    $.ajax({
        url : 'connection.php',
        type : 'POST',
        data : {'name' : 'Ben'},
        success : function(response){
            console.log(response);
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log('jqXHR.responseText:  ' + jqXHR.responseText);
            console.log('jqXHR.responseXML :  ' + jqXHR.responseXML);
            console.log('textStatus:   ' +  textStatus);
            console.log('errorThrown:   ' + errorThrown);
        },
        dataType : 'text' //expected data type
    });
}

Here, the I have type = 'POST' . When I run testAjax() on console, it gives me the following:

jquery-3.1.0.js:9392 POST https://(root address)/connection.php 404 (Not Found)send @ jquery-3.1.0.js:9392ajax @ jquery-3.1.0.js:8999testAjax @ script.js:63(anonymous function) @ VM3304:1
script.js:71 jqXHR.responseText:  Cannot POST /jkeezie/homework-checker/WIP/connection.php

script.js:72 jqXHR.responseXML :  undefined
script.js:73 textStatus:   error
script.js:74 errorThrown:   Not Found

However, when I change it to type = 'GET', and get rid of the data : {'name' : 'Ben'}, and run the same line on console, I get this:

<?PHP
    echo "haha";
?>

The questions:

  1. Why does it give me the 404 (first line of the response in the first scenario) when I use type = 'POST' and go through fine when I use type = 'GET'?

  2. Why does the second scenario return the entire PHP file content instead of just "haha" (without the quote marks).

J.Ko
  • 964
  • 12
  • 26
  • 2
    To the second question: That you get the content of the php file means that php is not activated on your server or for the given directory. To the first question: this indicated that POST request are not allowed in that directory. If PHP (or another server side language that processes the POST requests) is not enabled then it does not make sense to allow POST requests, so the first thing you need to do is to enable php. – t.niese Aug 14 '16 at 08:04
  • @t.niese How can I resolve this issue, then? Specifically, how can I configure so that POST requests are allowed in this directory? PHP is enabled by default in this IDE. Thank you so much for your help. – J.Ko Aug 14 '16 at 08:22
  • 1
    Php is **not** enabled on the server or at least the directory where you request `connection.php` file from, otherwise you would not see the ` – t.niese Aug 14 '16 at 08:25

1 Answers1

0

The answer to both questions is that you're downloading the connection.php file via a GET request to the server while what you're expecting is for the server to run the connection.php script instead.

Are you running any webserver? You have to set up one with PHP support to handle this requests as you would expect, being apache and nginx the most popular (and nginx the easier one)

martriay
  • 5,632
  • 3
  • 29
  • 39