I'm trying to send a JavaScript Object() to a PHP file in the correct format that $.POST wants. The PHP file doesn't set any $_POST[] variables so i must be sending it in an incorrect format.
JS:
$('#downloadBtn').click(function(){
var form_data = new Object();
form_data.filepath = $("#fileName").html();
$.post(
"/UpdateDownloads.php",
{ JSON.stringify(form_data) },
function(data) {
alert(data);
}
);
});
I know that changing the sent data to "{ filepath: form_data.filepath }" will fix the problem, but this is a sloppy fix because it doesn't change as i add more and more data to form_data. Basically im wondering if there is a JS function that can transform my Object() variable into a form which POST will accept and set the $_POST['filepath'] variables that i add to my form_data Object().
You can try sending the data as
{'data':JSON.stringify(form_data)}
Then on server side you will get $_POST['data']
which you can convert into object by using
json_decode($_POST['data']);
I hope this will work
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.