Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angular 5中发布XML正文?_Javascript_Angular_Ionic Framework_Ionic3 - Fatal编程技术网

Javascript 如何在Angular 5中发布XML正文?

Javascript 如何在Angular 5中发布XML正文?,javascript,angular,ionic-framework,ionic3,Javascript,Angular,Ionic Framework,Ionic3,我目前正在开发一个Ionic应用程序,它应该以XML为主体进行RESTAPI调用。我在使用XML发送邮件时遇到了问题。这是我的登录提供商。在将数据发布到API之前,我使用DOMParser将数据解析为XML import { HttpClient, HttpHeaders} from '@angular/common/http'; import { Injectable } from '@angular/core'; import * as Constants from '../../servi

我目前正在开发一个Ionic应用程序,它应该以XML为主体进行RESTAPI调用。我在使用XML发送邮件时遇到了问题。这是我的登录提供商。在将数据发布到API之前,我使用DOMParser将数据解析为XML

import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Injectable } from '@angular/core';
import * as Constants from '../../services/constants';

@Injectable()
export class LoginProvider {

  constructor(public http: HttpClient) {
  }

  postLogin(username : String, password : String) {
    let parser = new DOMParser();
    let xmlString = "<alm-authentication>  <user>" + username + "</user> <password>" + password + "</password> </alm-authentication>";
    let doc = parser.parseFromString(xmlString, "application/xml");
    console.log(doc);

    let headers = new HttpHeaders()
      .set('Access-Control-Allow-Origin', '*')
      .set('Content-Type', 'application/xml');
    return new Promise(resolve => {
      this.http.post(Constants.API_ENDPOINT + "/authentication-point/alm-authenticate", doc, {headers: headers}).subscribe(data => {
        resolve(data);
      }, err => {
        console.log(err);
      });
    });
  }

}
但当我在Google Chrome中检查post请求时,我得到以下信息:

请求负载似乎是JSON而不是XML。如何让它真正发送XML文件

我已经尝试将主体更改为xml字符串而不是文件,并更改内容类型,但仍然会出现相同的错误

我正在使用离子角度版本3.9.2和角度版本5.0.3

doc是表示XML文档的DOM的对象

您正在将doc作为参数传递给http.post

由于您正在传递一个对象,Angular正在尝试将其转换为JSON

如果您自己对请求负载进行编码,即不希望将其转换为JSON,则需要传递一个字符串


传递xmlString而不是doc。

这可能会有帮助。@MuhammadSaifuddin该线程似乎正在使用Cordova。因为我没有使用它,所以我希望有一个不同的解决方案..设置'Access-Control-Allow-Origin','*'-这是一个响应头,它不需要在请求头上。移除它。
let doc = parser.parseFromString(xmlString, "application/xml");
http.post(Constants.API_ENDPOINT + "/authentication-point/alm-authenticate", doc, {headers: headers})