Reputation: 35973
Hi I have a stie developed in codeigniter and I want to send an xml by an ajax call. The xml is come from another server. This is the ajax in my view
xmlDoc.loadXML(xmlfromserver);
$(function(){
$.ajax({
type: "POST",
url: "<?php echo site_url('/backend/provider/all_country_request'); ?>",
data: "xml"+xmlDoc.xml,
async: false,
contentType: "text/xml",
dataType: "text",
success: function(msg)
{
alert(msg);
},
error: function()
{
alert("error");
}
});
});
This is my controller:
public function all_country_request(){
if ($this->User_model->isLoggedIn()){
$this->Travco_model->all_country_request();
}
else{
redirect('/backend/user/home/');
}
}
and this is my simple model:
function all_country_request(){
$xml_str = $_POST['xml'];
$xml = new SimpleXMLElement($xml_str);
foreach ($xml->DATA as $entry){
$data = array(
'currency_code_travco'=>$entry->attributes()->CURRENCY_CODE,
'currency_name'=>$entry->CURRENCY_NAME,
'created'=>date('Y-m-d H:i:s'),
'modified'=>date('Y-m-d H:i:s'),
);
$this->db->insert('currency_travco',$data);
echo '<br>';
}
}
This my XML:
<?xml version="1.0" standalone="yes"?>
<RETURNDATA lang="it-IT" type="COR" xsi:noNamespaceSchemaLocation="http://xmlv5test.travco.co.uk/trlink/schema/CountryRequestV6Rcv.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MESSAGE>All Countries details and relevant city details</MESSAGE>
<DATA COUNTRY_CODE="ABW" CURRENCY_CODE="EUR">
<COUNTRY_NAME>Aruba</COUNTRY_NAME>
<CURRENCY_NAME>euro</CURRENCY_NAME>
</DATA>
The error that return to me is in the image attached
What is the problem?
Upvotes: 1
Views: 978
Reputation: 197757
Any kind of input your PHP scripts will take you need to properly validate before you continue.
In your case a more verbose variant could look like this:
try {
if (!isset($_POST['xml'])) {
throw new Exception('Missing Parameter Attribute "xml"');
}
$mode = libxml_use_internal_errors(true);
$xml = new SimpleXMLElement($_POST['xml']);
} catch (Exception $e) {
$code = 400;
$phrase = 'Bad Request';
header(sprintf('HTTP/ %d %s', $code, $phrase), true, $code);
$response = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><response/>');
$response->status->phrase = $phrase;
$response->status->code = $code;
$response->message = $e->getMessage();
if ($errors = libxml_get_errors()) {
$responseErrors = $response->addChild('errors');
foreach($errors as $error) {
$responseError = $responseErrors->addChild('error');
foreach($error as $name => $value) {
$value && $responseError->$name = rtrim($value);
}
}
}
header('Content-Type: application/my-app-response-bucket+xml; charset=utf-8');
$response->asXML('php://STDOUT');
return;
}
foreach ($xml->DATA as $entry) {
$data = array(
'currency_code_travco' => $entry->attributes()->CURRENCY_CODE,
'currency_name' => $entry->CURRENCY_NAME,
'created' => date('Y-m-d H:i:s'),
'modified' => date('Y-m-d H:i:s'),
);
$this->db->insert('currency_travco', $data);
echo '<br>';
}
...
For the error-condition of serving an request that is missing or invalid data (the Bad Request) a proper response is given back. It even has a response body consisting of XML highlighting errors, for example missing data:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<phrase>Bad Request</phrase>
<code>400</code>
</status>
<message>Missing Parameter Attribute "xml"</message>
</response>
or in case of non-well-formed XML (<?xml ?><fa ke></fa>blurb
) provided:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<phrase>Bad Request</phrase>
<code>400</code>
</status>
<message>String could not be parsed as XML</message>
<errors>
<error>
<level>3</level>
<code>96</code>
<column>6</column>
<message>Malformed declaration expecting version</message>
<line>1</line>
</error>
</errors>
<errors>
<error>
<level>3</level>
<code>41</code>
<column>13</column>
<message>Specification mandate value for attribute ke</message>
<line>1</line>
</error>
</errors>
<errors>
<error>
<level>3</level>
<code>5</code>
<column>16</column>
<message>Extra content at the end of the document</message>
<line>1</line>
</error>
</errors>
</response>
Upvotes: 1
Reputation: 373
To Send an xml document as data to the server. You Must be set the processData option to false, the automatic conversion of data to strings is prevented. for example :
$.ajax({
url: "page.php",
processData: false,
data: xmlDocument
});
Good Luck,
Upvotes: 1