繁体   English   中英

离子 Http 请求获取 XML 对象错误

[英]Ionic Http request getting error with XML object

当我尝试将 XML 对象发送到 Geoserver 时遇到问题。 当我尝试使用 Postman 时,它成功发送,即使在 Geoserver Demo 中也是如此。 但是用我的模拟器我得到了这些错误。

我已经尝试过来自'@ionic-native/http/ngx'; HTTPClient、XMLHttpRequest 和 HTTP '@ionic-native/http/ngx'; 但我没有得到很好的反馈。

使用 XMLHttpRequest 和 HttpClient 我收到此错误:

CORS 策略阻止了在 ' http://ip_address:port/geoserver/workapce/wfs ' http://ip_address:port/geoserver/workapce/wfs处访问 XMLHttpRequest 来自源 ' http://localhost ' 的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:否 '访问-Control-Allow-Origin' 标头存在于请求的资源上。

并使用来自'@ionic-native/http/ngx'; 我得到这个

错误:advanced-http: "data" 选项配置为仅支持以下数据类型: Object at Object.processData (helpers.js:400) at Object.sendRequest (public-interface.js:158) at Object.post (public -interface.js:174) at callCordovaPlugin (vendor-es2015.js:98821) at vendor-es2015.js:98636 at vendor-es2015.js:98594 at new ZoneAwarePromise (polyfills-es2015.js:3882) atvendor tryNativePromise -es2015.js:98593) 在 getPromise (vendor-es2015.js:98614) 在 wrapPromise (vendor-es2015.js:98619)

这是我的代码

 var xmlData= '<wfs:Transaction service="WFS" version="1.0.0" '+
   'xmlns:topp="http://www.openplans.org/topp" '+
   'xmlns:ogc="http://www.opengis.net/ogc" '+
   'xmlns:wfs="http://www.opengis.net/wfs">'+
   '<wfs:Update typeName="topp:tasmania_roads">'+
     '<wfs:Property>'+
      '<wfs:Name>TYPE</wfs:Name>'+
      '<wfs:Value>street</wfs:Value>'+
    '</wfs:Property>'+
    '<ogc:Filter>'+
      '<ogc:FeatureId fid="tasmania_roads.1"/>'+
    '</ogc:Filter>'+
  '</wfs:Update>'+
'</wfs:Transaction>';

//Request using HttpCLient

let doc = parser.parseFromString(xmlData, "application/xml");

let headers = new HttpHeaders()
.set("Authorization", btoa("admin:geoserver"))
.set('Access-Control-Allow-Origin', '*')
.set('Content-Type', 'application/xml');
this.httpClient.post(this.ENDPOINT + 'geoserver/workspace/wfs', doc, {headers: headers, 
withCredentials : true, observe : 'body'})
.subscribe((rs) => {
console.error(rs);
})

//Request using HTTP

this.httpNative.post(this.ENDPOINT + 'geoserver/workspace/wfs',xmlData , {
Authorization: btoa("admin:geoserver"), 'Content-Type': 'application/xml',
}
).then((rs) => {
console.log(rs);
}).catch((err) => {
console.error(err);
})

需要帮助来解决它。

尝试这个:

    let headers = {
      // Specifying that we will send XML.
      "Content-type": 'text/xml;charset="utf-8"',

      // Specifying that we will except XML.
      "Accept": "text/xml",
    };

    // Setting the data serializer as "utf8" is very important so that
    // the HTTP will know that the body will not be a JSON object, but
    // a unicoded string which is the XML.
    this.http.setDataSerializer("utf8");
    
    let xmlString = '<wfs:Transaction service="WFS" version="1.0.0" ' +
      'xmlns:topp="http://www.openplans.org/topp" ' +
      'xmlns:ogc="http://www.opengis.net/ogc" ' +
      'xmlns:wfs="http://www.opengis.net/wfs">' +
      '<wfs:Update typeName="topp:tasmania_roads">' +
      '<wfs:Property>' +
      '<wfs:Name>TYPE</wfs:Name>' +
      '<wfs:Value>street</wfs:Value>' +
      '</wfs:Property>' +
      '<ogc:Filter>' +
      '<ogc:FeatureId fid="tasmania_roads.1"/>' +
      '</ogc:Filter>' +
      '</wfs:Update>' +
      '</wfs:Transaction>';

    this.http.post('https://...', xmlString, headers).then(response => {

      console.log("Response", response);
      console.log("Response Data", response.data);
    }).catch(error => {
      console.log(error);
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM