[英]Error XML on ajax POST
嗨,我在codeigniter中開發了stie,我想通過ajax調用發送xml。 xml來自另一台服務器。 我認為這是阿賈克斯
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");
}
});
});
這是我的控制器:
public function all_country_request(){
if ($this->User_model->isLoggedIn()){
$this->Travco_model->all_country_request();
}
else{
redirect('/backend/user/home/');
}
}
這是我的簡單模型:
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>';
}
}
這是我的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>
回到我的錯誤在所附圖片中
問題是什么?
將xml文檔作為數據發送到服務器。 您必須將processData選項設置為false ,以防止將數據自動轉換為字符串。 例如 :
$.ajax({
url: "page.php",
processData: false,
data: xmlDocument
});
祝好運,
您的PHP腳本的任何輸入都將需要您在繼續之前正確進行驗證。
在您的情況下,更冗長的變體可能如下所示:
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>';
}
...
對於服務缺失或無效數據的請求 ( 錯誤請求 )的錯誤條件,將返回適當的響應。 它甚至具有一個包含XML高亮顯示錯誤的響應正文,例如缺少數據:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<phrase>Bad Request</phrase>
<code>400</code>
</status>
<message>Missing Parameter Attribute "xml"</message>
</response>
或提供格式不正確的XML( <?xml ?><fa ke></fa>blurb
):
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.