简体   繁体   English

如何使用XHR获取PHP文件内容?

[英]How can I get PHP file contents using XHR?

How can I get PHP file contents using XHR? 如何使用XHR获取PHP文件内容? I get empty response... 我得到空的回应...

const promise = new Promise(function (success, error) {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);

    xhr.onreadystatechange = function () {
        if (xhr.readyState !== 4) return;
        if (xhr.status === 200) return success(xhr.responseText);
        return error(new Error(`Failed lo load language data. Server response status: ${xhr.status}.`));
    };

    xhr.send();
}.bind(this));

PHP file contents that I need to get as text: 我需要以文本形式获取的PHP文件内容:

$_text_block_users = 'Заблокированные ученики';
$_text_id_block_user = 'ID';
$_text_firstname_lastname_block_user = 'Имя, фамилия';
$_text_group_block_user = 'Группа';
$_text_email_block_user = 'Почта';
$_text_contact_person_block_user = 'Контактное лицо';
$_text_telephone_block_user = 'Телефон';
$_text_actions_block_user= 'Действия';

HTTP request to PHP file executes it and returns response (empty in my case). 对PHP文件的HTTP请求将执行该请求并返回响应(在我的情况下为空)。 I think it is needed to set some HTTP headers. 我认为需要设置一些HTTP标头。


The only solution I found is to use simple AJAX. 我发现的唯一解决方案是使用简单的AJAX。 I call some PHP file, get aim PHP file contents with 'file_get_contents()' function and return this contents to the client side (JS). 我调用一些PHP文件,使用'file_get_contents()'函数获取目标PHP文件内容,并将此内容返回给客户端(JS)。 But I am not pleased with this solution... Hope someone know how to make it in most elegant way. 但是我对这种解决方案不满意……希望有人知道如何以最优雅的方式进行制作。

The only solution I found is to use simple AJAX. 我发现的唯一解决方案是使用简单的AJAX。 I call some PHP file, get aim PHP file contents with 'file_get_contents()' function and return this contents to the client side (JS). 我调用一些PHP文件,使用'file_get_contents()'函数获取目标PHP文件内容,并将此内容返回给客户端(JS)。 But I am not pleased with this solution... Hope someone know how to make it in most elegant way. 但是我对这种解决方案不满意……希望有人知道如何以最优雅的方式进行制作。

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

相关问题 如何使用XHR获得SpreadSheet Google? - How can I get a SpreadSheet Google using XHR? 如何在没有xhr的情况下获取包含的js文件的内容? - How to get an included js file's contents without xhr? 如何像在 php 中一样在 javascript 中使用 file_get_contents(url)? 有什么替代方法吗? - How can I use file_get_contents(url) in javascript just like in php? Is there any alternative? 我可以告诉我的 php 函数在“file_get_contents”使用 php 代码中的 javascript 加载页面之前不执行吗? - Can I tell my php function to not execute until the "file_get_contents" has loaded the page using javascript in php code? 如何<a>使用php file_get_contents()</a>更改<a>标签href</a> - How to change <a> tag href using php file_get_contents() 如何在 puppeteer 中获取所有 xhr 调用? - How can I get all xhr calls in puppeteer? 如何使用 chrome-extension 收听 xhr 响应? - How can I listen to xhr response using chrome-extension? 如何使用jasmine模拟XHR中的超时事件? - How can I simulate a timeout event in a XHR using jasmine? 如何判断XHR是否使用readyState发送到服务器? - How can I tell XHR is sent to server using readyState? 使用puppeteer,我如何等待特定的xhr请求完成 - Using puppeteer, how can i wait for specific xhr request to be complete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM