gpt4 book ai didi

Receive data from client-side on server-side(在服务器端从客户端接收数据)

转载 作者:bug小助手 更新时间:2023-10-24 21:43:52 32 4
gpt4 key购买 nike



I have an html page where clients insert a url so that it can be stored on a file on the server side.

我有一个html页面,在那里客户端插入一个url,这样它就可以存储在服务器端的一个文件中。


The issue that I am having is that I cannot figure out how to receive client data on the server

我遇到的问题是我不知道如何在服务器上接收客户端数据


Here is my html script

以下是我的html脚本


<!DOCTYPE html>
<html>
<body>
<input type="url" id="link"><br>
<button onclick="myFunction()">Download</button>

<script>
function myFunction() {
var url= document.getElementById("link").value
}

</script>
</body>
</html>

How can I send the data of the "url" variable to my server?

如何将“url”变量的数据发送到我的服务器?


I am using apache so it naturally supports php

我使用的是apache,所以它自然支持php


Inside the function I tried to use fetch in combination with a php file

在函数中,我尝试将FETCH与一个php文件结合使用


function myFunction() {
var url= document.getElementById("link").value
fetch(".php", {
"method": "POST",
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"body": JSON.stringify(url)
}).then(function(response){
return response.text();
}).then(function(data){
console.log(data);
})
}

And the php file

和php文件


<?php
if(isset($_POST)){
$data = file_get_contents("php://input");
$link = json_decode($data);
echo $link ;
}

I was expecting to receive the url from the text input but instead the console.log returned the whole html script

我原本希望从文本输入中接收url,但却返回了整个html脚本。


更多回答

I can't see any php file specified in your question above.

我看不到您上面问题中指定的任何php文件。

优秀答案推荐

In your script, just update the fetch(".php", { line.
You need to give a file name here.

在您的脚本中,只需更新FETCH(“.php”,{line.您需要在此处提供一个文件名。


For example, if the name of the PHP file which processes the data is server.php, you have to write fetch("server.php", {

例如,如果处理数据的PHP文件的名称是server.php,则必须编写FETCH(“server.php”,{


This should solve your issue.

这应该可以解决你的问题。


更多回答

Such a silly mistake from me, thank you very much sir!

我犯了这样一个愚蠢的错误,非常感谢先生!

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com