获取python或php文件作为源代码出现,而不是我所需的响应

发布于 2025-01-27 21:47:48 字数 774 浏览 2 评论 0原文

js:

fetch('app.py').then(res=>res.text()).then(data=>console.log(data));

python:

print('helloworld')

console.log()显示print> print('helloworld')而不是helloworld

我不是在这里得到东西吗?我希望Python代码返回“ Helloworld”到浏览器的控制台,而是发送Sourcecode。与PHP相同(请注意,CMD中的php -version发送php警告:'vcruntime140.dll'14.0不是compat ..)。但是Python在我的PC上似乎没有任何问题。

我的代码问题是我的问题吗?或app.py或app.php在我获取时需要什么?

嗨,我是一个年轻的程序员,一直在尝试将脚本添加到我的服务器中,以让客户端将数据发送到服务器端,其中 server -Side 的工作是将数据接收到我的服务器的文件中。谢谢

Js:

fetch('app.py').then(res=>res.text()).then(data=>console.log(data));

Python:

print('helloworld')

console.log() shows print('helloworld') instead of helloworld

Am I not getting something here? I want the Python code to return "helloworld" to the browser's console, but instead it sends the sourcecode. Same with php(note that php --version in cmd sends PHP Warning:'vcruntime140.dll' 14.0 is not compat..). But python appears to have no issues on my pc.

Is my problem in my code? Or what is necessary for the app.py or app.php to run when I fetch it?

Hi, I'm a young programmer, have been trying to add scripts to my server to let client-side send data to server-side, in which server-side's job is to receive and write data into files to my server. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2025-02-03 21:47:48

代码所做的只是获取本地文件(app.py),然后将其内容打印到浏览器控制台。这不是API的工作方式。您可以在烧瓶中创建一个API端点(用于编写API的Python库),该端子将将Helloworld打印到单独的(服务器)控制台,或将其返回为数据。遵循本指南以使您入门(您可能需要首先按照安装说明进行操作 - 页面上的链接) https://flask.palletsprojects.com/en/1.1.x/quickstart/
您的获取声明看起来像这样
fetch('/')。然后(res => res.text())。然后(data => console.log(data));

您的app.py看起来如下:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

我对PHP不熟悉,所以害怕我不能在那里给您指针。

All that code is doing is fetching a local file (app.py) and then printing its contents to the browser console. This isn't how api's work. You can create an api endpoint in Flask (a Python library for writing apis) that will print helloworld to a separate (the server) console, or return it as data. Follow this guide to get you started (you may want to follow the installation instructions first - link on the page) https://flask.palletsprojects.com/en/1.1.x/quickstart/
Your fetch statement will look like this
fetch('/').then(res=>res.text()).then(data=>console.log(data));

your app.py will look as follows:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

I am not familiar with php so afraid I can't give you pointers there.

甚是思念 2025-02-03 21:47:48

无法在客户端上运行 Python或PHP文件,这必须在后端上完成。对于Python,您需要运行一个聆听传入请求的脚本(服务器),并在此发送响应。 PHP也是如此,您可以使用Apache,Nginx,LightTPD等Web服务器运行PHP。

似乎您有一个用于获取文件的静态服务器,但是再次,您无法请求文件并期望它运行。

You cannot run Python or PHP files on the client side, this has to be done on the backend. For Python you need to run a script (server) that listens for incoming requests, and on that send a response. The same applies to PHP, you can use web servers like Apache, Nginx, Lighttpd and more to run php.

Seems like you have a static server for files fetching, but again, you cannot request the file and expectate it to run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文