简体   繁体   中英

Ajax jQuery with FormData and string values

Is it possible to pass a jQuery AJAX request that contains both a FormData object as well as a string value? Below are my unsuccessful attempts so far. I keep getting the same error:

Undefined index: check

when I attempt to fetch the value like this:

$test = $_POST['check'];
echo $test;

My two attempts are as seen below:

var formData = new FormData(document.getElementById('#form1'));
var bar = "salesReport";

$.ajax({
        type        : 'POST', 
        url         : 'process.php',
        data        : {
                        formData,
                        check : bar
                            }, 
        dataType    : 'json', 
        processData : false,
        contentType : false,
        encode      : true
    })

or this:

var formData = new FormData(document.getElementById('#form1'));

formData.append("check", "salesReport");

$.ajax({
        type        : 'POST', 
        url         : 'process.php',
        data        : formData,
        dataType    : 'json', 
        processData : false,
        contentType : false,
        encode      : true
    })

If you have a php backend you may have to parse it from the input stream

$request_body = file_get_contents('php://input');
$_POST = (array)(json_decode($request_body, true));

then you can use the $_POST again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM