I am trying to send XML file via XmlHttpRequest but unfortunately nothing is sent,
when I print $_POST
it gives me an empty array.
I tried two ways to create XML
- createDocument
- XMLstring then parseFromString to xml.
but to no avail.
I think the reason is the header:
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
because when I replace it with this header
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
it works but it sends string not xml file I know that I can send xml string to my php then parse it to xml file but I don't want this way, I need to send xml file directly
even if i changed header to text/xml and used in php
$rawData = file_get_contents("php://input");
echo gettype ($rawData);
it returns string so I have to use simplexml_load_string to convert it to object this is my js code:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","http://localhost/dashboard/webservice/SOAP/soapServer/ex.php");
var xmlDoc;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
xmlDoc = xmlhttp.response;
console.log(xmlDoc);
}
};
//xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
//way1
var xmlString = "<?xml version='1.0'?><root><query><author>John Steinbeck</author></query></root>";
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlString, "text/xml");
//way2
var doc = document.implementation.createDocument("", "", null);
var peopleElem = doc.createElement("people");
var personElem1 = doc.createElement("person");
personElem1.setAttribute("first-name", "eric");
personElem1.setAttribute("middle-initial", "h");
personElem1.setAttribute("last-name", "jung");
xmlhttp.send(xmlDoc);
php code ,so simple:
?php
header("Access-Control-Allow-Origin: http://localhost:3000");
var_dump($_POST);
?>