简体   繁体   中英

opening dynamic php page with ajax

I have a php script that create user specific pdf files. (mpdf)

To download the file without losing the current page i used ajax.

var jsonString = JSON.stringify(multydimarray);
$.ajax({
  type: "POST",
  url: "gen.php",
  data: {data : jsonString}

  success: function(response){
   window.location = "gen.php";
  }
});

But gen.php wasn't received any data from ajax. $_POST['data'] wasn't set So,

File was downloaded, Current page stayed untouched, but the file was empty.

Any suggestion?

specify the ajax request's contentType to application/json; charset=utf-8 application/json; charset=utf-8

$.ajax({
  type: "POST",
  url: "gen.php",
  data: {data : jsonString}
  contentType: "application/json; charset=utf-8",

  success: function(response){
   window.location = "gen.php";
  }
});

then get the data in PHP like this

$data = json_decode(file_get_contents('php://input'));

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