1

嗨,我有一个在 codeigniter 中开发的 stie,我想通过 ajax 调用发送一个 xml。xml 来自另一台服务器。这是我认为的ajax

            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>

返回给我的错误在附图中 在此处输入图像描述

问题是什么?

4

2 回答 2

1

将 xml 文档作为数据发送到服务器。您必须将processData选项设置为false,以防止数据自动转换为字符串。例如 :

$.ajax({
  url: "page.php",
  processData: false,
  data: xmlDocument
});

祝你好运,

于 2013-03-08T12:13:45.697 回答
1

在继续之前,您需要正确验证您的 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>
于 2013-03-08T13:22:29.873 回答