How to solve the problem of Array ([id] => [object Object]) when the PHP backend takes specific values

Encountered when fetching front-end data from the php back-end

Array ( [id] => [object Object] )

Yesobj type

First look at the code, the problem I encountered

index.js

 return (
        <Dropzone
            accept="image/*,audio/*,video/*,.pdf"
            getUploadParams={
    
    ({
    
    file, meta}) => {
    
    

                const body = new FormData();
                body.append('fileField', file);
                body.append('id', id);

                console.log(file);
                console.log(id);
                return {
    
    url: 'http://localhost/cup/u_upload_img.php', body}
            }}

            onSubmit={
    
    handleSubmit}
            InputComponent={
    
    Input}
            getFilesFromEvent={
    
    getFilesFromEvent}
            submitButtonContent="submit"
        />
    )

I used it in the above code, console.log()and the figure below is the result

Insert picture description here
server.js

<?php
    $json = json_encode($_POST);
    //print_r($json);

    $obj = json_decode($json, true);
    print_r($obj);

    $id = $obj->id;
    echo $id;
    

But the result obtained by network isArray ( [id] => [object Object] )

Insert picture description here


Solution:

First of all, we have to know that our console.log(id)result is an object. If we look at the picture above, we will know that it is contained by {}

So I want to take the idcorresponding testvalue from the object ,

Directly body.append('id', id.id);on it

That is, console.log(id.id)you can get testvalue

Okay, so the front end is over

Change the code of the backend a little bit

$json = json_encode($_POST);
//print_r($json);
$obj = json_decode($json, true);
print_r($obj);
//var_dump($json);
$id = $obj['id'];
echo $id;


Yes, that's it. Thanks to the enthusiastic friends of Stack Overflow, it can be solved.

I thought about this for more than 2 days and finally solved it. If you want to see my dialogue with him, click here

Guess you like

Origin blog.csdn.net/weixin_43814775/article/details/108333118