繁体   English   中英

Electron AJAX请求返回原始PHP

[英]Electron AJAX request return raw PHP

我正在用Electron开发信息亭应用程序。 目前,我一直坚持使用本地Javascript代码( 不使用JQuery )向服务器发出AJAX请求。 每次我提出AJAX请求时,它总是返回原始PHP代码。 这是我的项目目录:

SelfService
|- script
   |- login.js
|- lib
   |- userAuth.php
|- node_modules
|- index.html
|- index.js
|- package.json
|- package-lock.json

在我的JavaScript中,我尝试将“ Content-Type”设置为“ application / json”,或者在网上遵循AJAX请求的另一个示例,将其设置为“ application / x-www-form-urlencoded”,然后再发送XMLHttpRequest,但是他们两个仍然返回了原始PHP代码。

至于服务器端,我已经将标头设置为“ Content-Type:application / json”,并使用json_encode作为结果。

侧面说明:我将Electron安装在与Web服务器不同的驱动器上。 我在默认位置(C:\\wamp)上安装了WAMP,而在D:驱动器上安装了Electron(我想知道这是否真的很重要)

index.html的:

<body>
    <input type="text" id="input_id" value="" />
    <button onclick="authenticateID();">Click to authenticate ID</button>
    <div id="response"></div>
</body>

/script/login.js:

function authenticateID () {
    var xhr = new XMLHttpRequest();
    var url = 'lib/userAuth.php'
    var params = 'id=' + document.getElementById('input_id').value;

    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-Type', 'application/json');
//    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

    xhr.onload = function () {
        if (this.readyState == 4 && this.status == 200) {
            var xhrResult = JSON.parse(xhr.responseText);

            var responseContainer = document.getElementById(response);
            responseContainer.innerHTML = xhrResult.isValidID;
        }
    }

    xhr.onerror = function () { alert('Something went wrong') }
    xhr.send(params);
}

/lib/userAuth.php:

<?php
header('Content-Type: application/json');
$result['isValidID'] = (!empty($_POST['id'])) ? '1' : '0';
echo json_encode($result);
?>

如果用户在文本框中输入ID,我希望输出将为“ 1”或“ 0”,但是当我显示带有alert(xhr.responseText) ,它将返回的原始PHP代码。 lib/userAuth.php

编辑:现在,这是我犯的一个巨大错误。 尽管Electron具有某种可以处理PHP文件的内置Web服务器,但是我将PHP文件放在了项目文件夹中,该文件夹与Web服务器的位置不同。 现在,我已将PHP脚本分离到Web服务器中,如何在var url = 'lib/userAuth.php'设置URL? 我尝试了localhost/SelfService/lib/userAuth.php但是在错误日志中,它显示ERR:FILE_NOT_FOUND

服务器返回PHP代码的唯一原因是服务器配置不正确。 检查您的服务器文件是否驻留在localhost服务器文件夹中。

暂无
暂无

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

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