0

i am getting this error (index):31 POST https://amrit.github.io/scripts/sendData.php net::ERR_FAILED 405

function ajaxpost() {
            // (A) GET FORM DATA
            var form = document.getElementById("myForm");
            var data = new FormData(form);

            // (B) AJAX
            var xhr = new XMLHttpRequest();    
            xhr.open("POST", "https://amrit.github.io/scripts/sendData.php");
            //       

            //xhr.open("POST", "scripts/sendData.php");
            // What to do when server responds
            xhr.onload = function() {
                console.log(this.response);
            };
            xhr.send(data);

        }

i am getting following error when i deploy the html on website. Uncaught SyntaxError: Unexpected token '<', "<?php "... is not valid JSON at JSON.parse () at xmlhttp.onreadystatechange

function get_component_states() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "scripts/receiveData.php", true);
            xmlhttp.send();           
            xmlhttp.onreadystatechange = function() {
              
                if (this.readyState == 4 && this.status == 200) {
                    // update the form with the new values
                    // stringify the response
                    var response = JSON.parse(this.response);
                    //var response = JSON.parse(this.response);
                    console.log(response);
                    // update the form with the new values
                    
                    document.getElementsByName("mainVoltage")[0].innerHTML = response.mainVoltage;
                    document.getElementsByName("loadPower")[0].innerHTML = response.loadPower;
                    document.getElementsByName("exportPower")[0].innerHTML = response.exportPower;
                    document.getElementsByName("solarPower")[0].innerHTML = response.solarPower;
                    document.getElementsByName("todayKwh")[0].innerHTML = response.todayKwh;
                    document.getElementsByName("totalKwh")[0].innerHTML = response.totalKwh;
                    document.getElementsByName("circuit1")[0].checked = response.circuit1;
                    
                }
                // show the response in the console
                console.log(this.response);
                
            };
            
        }
Amrit
  • 19
  • 5
  • 1
    Fix your PHP, it has syntax errors or throws non-json error. It has nothing to do with JS – Justinas Sep 22 '22 at 10:56
  • For me `POST https://amrit.github.io/scripts/sendData.php` returns HTML, not JSON. Error is shown `405 Not Allowed` - [HTTP 405](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405) – Justinas Sep 22 '22 at 10:58
  • I am sorry I am not sure how to fix it – Amrit Sep 22 '22 at 11:02

1 Answers1

2

Github Pages does not allow POST requests and cannot execute PHP either. It's only for displaying simple, static or Javacript-driven pages, with no backend.

References:

GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website

Github pages currently do not support PHP as it only supports static website.

Instead, deploy your application to a hosting environment which provides the correct environment for the code you want to host. There are thousands of hosting providers around the world which provide a PHP-capable environment.

ADyson
  • 57,178
  • 14
  • 51
  • 63