I am trying to build a function that exports and XML as download. I use the XMLWriter
function from PHP which works great. I actually get the result I want, only, it comes back in my AJAX response rather than it's opted as a download, which i told the script to do so.
My layout is as followed:
JS + AJAX
// ///////////////////////////// //
// START EXPORT //
// ///////////////////////////// //
function exports(vars){
$.ajax({
url: domain + "/core/ajax/export.php",
type: "post",
dataType: "text",
data: vars,
success: function(data){
if(vars.form == 'export-records'){
}
},
error: function(jqXhr, textStatus, errorMessage){
console.log("Error: ", errorMessage);
}
});
}
$('#export-propperties').bind('submit',function(){
event.preventDefault();
var content = $('#export-propperties').serializeArray().reduce(function (newData, item) {
if (item.name.substring(item.name.length - 2) === '[]') {
var key = item.name.substring(0, item.name.length);
if(typeof(newData[key]) === 'undefined') {
newData[key] = [];
}
newData[key].push(item.value);
} else {
newData[item.name] = item.value;
}
return newData;
}, {});
exports(content);
});
So what happens here is i submit my form as a key value pair into my ajax function. Works fine. The php proces is as follows.
// START THE XML HERE
// AFTER THE DATA IS FETCHED WE WILL PARSE IT NORMALLY. WE ARE GOING TO USE THE RESULT LATER, FIRST WE CREATE THE INSTANCE OF THE FILE
// TO DEFINE FIRST THE XML
$writer = new XMLWriter();
$writer->openURI('php://output');
$writer->startDocument('1.0','UTF-8');
$writer->setIndent(4);
// CREATE THE HEADING OF THE PRODUCTDATA
$writer->startElement('ProductData');
$writer->writeAttribute('xmlns:xs', 'http://www.w3.org/2001/XMLSchema');
$writer->writeAttribute('xmlns', 'http://www.gs1.nl/productgegevens/insbou/004');
$writer->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$writer->writeAttribute('xsi:schemaLocation', 'http://www.gs1.nl/productgegevens/insbou/004 Productgegevens_insbou004.xsd');
// blablabla xml generation //
$writer->endElement();
// END OF DOCUMENT
$writer->endDocument();
$writer->flush();
The problem is that my response of the code is XML, rather than the wanted XML download ($writer->openURI('php://output');
). I have a feeling I cant execute the download window within the AJAX call, or am I wrong here? How can I solve this?