将数组从JavaScript提交到PHP 首先,我将localStorage中的数组放入变量中 var url=JSON.parse(window.localStorage.getItem(“URL”) 现在我需要在同一个页面中将这个数组转换为php中的一个变量 我尝试的方式
将数组从JavaScript提交到PHP 首先,我将localStorage中的数组放入变量中 var url=JSON.parse(window.localStorage.getItem(“URL”) 现在我需要在同一个页面中将这个数组转换为php中的一个变量 我尝试的方式,javascript,php,ajax,Javascript,Php,Ajax,$url=array() 对于JavaScript中的($i=0;$i),您可以获取 fetch("https://yourapi.api", { headers: { "Content-Type": "application/json", }, method: "POST", body: JSON.stringify({ u
$url=array()代码>
对于JavaScript中的($i=0;$i),您可以获取
fetch("https://yourapi.api",
{
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
url: [
"https://otherurl1.url",
"https://otherurl2.url",
"https://otherurl3.url"
]
})
});
如果window.localStorage.getItem(“url”)
是数组,则可以将url:JSON.stringify(window.localStorage.getItem(“url”)
在PHP中,您可以得到
$content = trim(file_get_contents("php://input")); //this will catch POST from JavaScript
$decoded = json_decode($content, true); //this will decode from JSON to PHP array
foreach($decoded['url'] as $url){
echo $url;
}
$decoded
将作为PHP数组作为您的数组
在PHP中,您还可以检查post是否作为JSON发送
if ($_SERVER["CONTENT_TYPE"] === "application/json") {
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
}
要将其视为函数/方法,JavaScript将如下所示
let fetchApi = () => {
fetch("https://yourapi.api",
{
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
url: window.localStorage.getItem("urls")
})
});
};
PHP,你可以在任何地方使用它
public static function get_data_from_js(){
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
$decoded = [];
if ($contentType === "application/json") {
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
}else{
//You can do here anything
}
return $decoded;
}
在JavaScript中,您可以获取
fetch("https://yourapi.api",
{
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
url: [
"https://otherurl1.url",
"https://otherurl2.url",
"https://otherurl3.url"
]
})
});
如果window.localStorage.getItem(“url”)
是数组,则可以将url:JSON.stringify(window.localStorage.getItem(“url”)
在PHP中,您可以得到
$content = trim(file_get_contents("php://input")); //this will catch POST from JavaScript
$decoded = json_decode($content, true); //this will decode from JSON to PHP array
foreach($decoded['url'] as $url){
echo $url;
}
$decoded
将作为PHP数组作为您的数组
在PHP中,您还可以检查post是否作为JSON发送
if ($_SERVER["CONTENT_TYPE"] === "application/json") {
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
}
要将其视为函数/方法,JavaScript将如下所示
let fetchApi = () => {
fetch("https://yourapi.api",
{
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
url: window.localStorage.getItem("urls")
})
});
};
PHP,你可以在任何地方使用它
public static function get_data_from_js(){
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
$decoded = [];
if ($contentType === "application/json") {
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
}else{
//You can do here anything
}
return $decoded;
}
如果数据是在JS中生成的,那么AJAX可能是最好的选择。在PHP中重用存储在同一页面上的变量中的数据是微不足道的,因此您如何使用它可能是个问题。但老实说,这里没有足够的信息来推动您向正确的方向发展。这是否回答了您的问题?要允许JavaScript向PHP发送数据,您需要必须向服务器发送请求,PHP可以在服务器上访问数据。以下是向服务器发布数据的简单方法:。@NicoHaase抱歉,它尚未完全解决。如果数据是在JS中生成的,那么AJAX可能是最佳选择。在PHP中重用存储在同一页面上的变量中的数据很容易,因此如何使用它可能是问题所在lem。但老实说,这里没有足够的信息来推动您朝正确的方向前进。这回答了您的问题吗?要允许JavaScript向PHP发送数据,您必须向服务器发送请求,PHP可以在服务器上访问数据。以下是一种向服务器发送数据的简单方法:。@NicoHaase抱歉,它尚未完全解决。您好,than谢谢你的回答。我从JS获取数组。没有问题。我在PHP中得到错误“警告:为foreach()提供的参数无效”。我无法使用PHP在LocalStorage中获取数据。您好,请使用print_r($decoded)测试发布的数据;然后再将其放入forach()以查看发布的数组中的内容。$decoded empty:(这意味着您没有向php发布数据。您能告诉我您的url是如何发布的吗?您好,谢谢您的回答。我从JS获取数组。没有问题。我在php中遇到错误“警告:为foreach()提供的参数无效”。我无法使用php在LocalStorage中获取数据。您好,请使用print\r测试发布的数据($decoded);在您将其放入forach()以查看发布的数组中的内容之前。$decoded empty:(这意味着您没有将数据发布到php。您能告诉我您的url是如何发布的吗?