简体   繁体   English

为什么我的 php 认为数组是字符串?

[英]Why does my php think that an array is a string?

I have this frontend javascript code, that collects information (an array) and sends it to the backend.我有这个前端 javascript 代码,它收集信息(一个数组)并将其发送到后端。 And for some reason, my php code believes this array is actually a string.由于某种原因,我的 php 代码认为这个数组实际上是一个字符串。 Here is my javascript code:这是我的 javascript 代码:

function gamesearch() {

    var text_search = document.getElementById("search-space").value;
    var creator_search = document.getElementById("search-space-creator").value;
    var tags = document.getElementsByClassName("selectedtag");

    var tagarray = ['banana', 'fruit'];

    console.log(tagarray);
    var fd = new FormData();
    fd.append("text_search", text_search);
    fd.append("creator_search", creator_search);
    fd.append("tagarray", tagarray);
    fd.append("gamesearch", true);
    var xhr = new XMLHttpRequest();
    var fullurl = '../backend/gamesearch.php';
    xhr.open('POST', fullurl, true);
    xhr.onload = function() {
        if (this.status == 200) {

            console.log(this.responseText);

        };
    };
        xhr.send(fd);

}

And here is my backend php code这是我的后端 php 代码

if (isset($_POST['gamesearch'])) {
  $text_search = $_POST['text_search'];
  $creator_search = $_POST['creator_search'];
  $tagarray = $_POST['tagarray'];

echo gettype($tagarray);

}

And in the console it says: string.在控制台中它说:字符串。 Please help!请帮忙! Thanks!谢谢!

Try stringifying your JSON array before sending it to the backend.尝试在将 JSON 数组发送到后端之前对其进行字符串化。

fd.append("tagarray", JSON.stringify(tagarray));

Then decode it back again in the backend然后在后端再次解码

$tagarray = json_decode($_POST['tagarray']);

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

相关问题 Javascript-为什么我的应用程序认为此数组为空? - Javascript - Why does my application think this array is empty? 为什么我的网站认为$ http请求是跨域的? - Why does my website think the $http request is cross domain? 为什么我的drupal 7网站认为以下变量不是函数? - Why does my drupal 7 site think the below variables are not functions? 为什么我的程序认为 72 大于 500? - Why does my program think that 72 is larger than 500? 为什么 Jade 认为来自我的数组的链接应该将用户路由到 /admin/thisurl 而不是实际链接? - Why does Jade think the link coming from my array should route the user to /admin/thisurl vs the actual link? 为什么我的PHP函数没有收到Ajax发送给它的数组 - Why my php function does not receive the array I sent to it by ajax 为什么扩展语法将我的字符串转换为数组? - Why does spread syntax convert my string into an array? 为什么我的代码为什么认为我的构造函数原型中的JavaScript函数未定义JavaScript? - Why does my code think that canvas functions are undefined in my constructor's prototype in JavaScript? 为什么罗马在我尝试编译它时认为我的文件是空的? - Why does Rome think my file is empty when I try to compile it? 为什么我的$ q决心和承诺不像我认为的那样工作? - Why doesn't my $q resolve and promise work like I think it does?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM