简体   繁体   English

XML HTTP GET请求失败

[英]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. 我正在对本地主机执行纯JS GET请求(不用担心我测试了POST并能正常工作),我可以访问所有数据库,但是GET请求将不起作用。 My JS code is shown below: 我的JS代码如下所示:

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文件中,我有以下代码:

<?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. POST请求所有工作并插入数据库,但是GET请求似乎停止了其下面的所有JS代码。 Any ideas why? 有什么想法吗? I have tried changing the last parameter true/false. 我尝试更改最后一个参数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. 另外,我正在尝试通过Intel XDK将其用于移动应用程序,因此如果有帮助,我将使用JS和PHP。 Please no jQuery solutions as that doesn't seem to work. 请不要使用jQuery解决方案,因为这似乎不起作用。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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