php 获取ajax raw,javascript - Electron AJAX request return raw PHP - Stack Overflow

I'm developing a kiosk application with Electron. Currently, I'm stuck with making an AJAX request to the server with a native Javascript code (no JQuery). Every time I made an AJAX request, it always returned the raw PHP code. Here is my project directory:

SelfService

|- script

|- login.js

|- lib

|- userAuth.php

|- node_modules

|- index.html

|- index.js

|- package.json

|- package-lock.json

In my javascript, I have tried to set the "Content-Type" to "application/json", or following another example of AJAX request on the net, set it to "application/x-www-form-urlencoded" before sending the XMLHttpRequest, but both of them still returned the raw PHP code.

As for the server side, I have already set the header as "Content-Type: application/json" and use json_encode for the result.

On the side note: I installed the Electron on different drive than my web server. I installed WAMP on default location (C:\wamp) while I installed the Electron on D: Drive (I wondered if that actually matters)

index.html:

Click to authenticate ID

/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:

header('Content-Type: application/json');

$result['isValidID'] = (!empty($_POST['id'])) ? '1' : '0';

echo json_encode($result);

?>

If the user typed in the ID in the textbox, I expect the output will be either "1" or "0", but when I show the xhr.responseText with alert(xhr.responseText), it returned the whole raw PHP code of lib/userAuth.php

EDIT: Now that's a huge mistake I made. I though Electron has some kind of built-in web servers that can process PHP file, so I put my PHP file in the project folder, which is in a different location from the web server. Now I have separated the PHP script into the web server, how am I supposed to set the URL in var url = 'lib/userAuth.php'? I tried localhost/SelfService/lib/userAuth.php but in error log, it says ERR:FILE_NOT_FOUND

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值