简体   繁体   中英

XML HTTP GET Request Fails

I'm doing a pure JS GET request, to my localhost (don't worry I tested POST and that works), I can access all the database and everything else but the GET request won't work. My JS code is shown below:

functtion getSessionKey() {
    XMLHTTP = new XMLHttpRequest();

    XMLHTTP.open("GET", "http://127.0.0.1:8080/projects/To-Do-App/api/sessionData/sessionKey.php", true);

    XMLHTTP.onreadystatechange = function () {
        if (XMLHTTP.readyState == 4) {
            alert(XMLHTTP.responseText);
        }
    }
    XMLHTTP.send(null);
}

In the PHP file I have the following code:

<?php
session_start();

if (isset($_SESSION['sessionKey']) && !empty(trim($_SESSION['sessionKey']))) {
    echo htmlspecialchars(trim($_SESSION['sessionKey']));
}
?>

The POST requests all work and insert into the database, but the GET request seem to stop all the JS code below it from working. Any ideas why? I have tried changing the last parameter true/false. Also I am trying to make it for a mobile application through Intel XDK so I using JS and PHP if that helps. Please no jQuery solutions as that doesn't seem to work.

You didn't set the content type

var XMLHTTP = new XMLHttpRequest();
XMLHTTP.setRequestHeader("Content-type","application/x-www-form-urlencoded");
...

and try to replace

XMLHTTP.send(null);

with

XMLHTTP.send();

不要使用.send()发送null

XMLHttp.send()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM