Angular 5 - HttpClient XML Post - сбой Http во время синтаксического анализа - PullRequest
0 голосов
/ 08 мая 2018

Я пытаюсь войти в локальный веб-сервис, используя xml:

Вот код:

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'text/xml',
    'Accept':  'text/xml',
    'Response-Type': 'text'
  })
};


login() {
    const postedData = `
        <authenticationDetail>
            <userName>myusername</userName>
            <password>mypassword</password>
        </authenticationDetail>`;
    return this.http.post('http://localhost/login.ws', postedData, httpOptions)
    .subscribe(
        result => {
            console.log('This result is' + result);
        },
        error => {
            console.log('There was an error: ', error);
        }
    );
}

Я получаю ошибку:

Http failure during parsing for 'http://localhost/login.ws'

В чем здесь проблема? Как я могу это исправить?

Ответы [ 3 ]

0 голосов
/ 08 мая 2018

Вы уже пробовали

const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type':  'text/xml',
        'Accept':  'text/xml',
        'Response-Type': 'text'
    }), 
    responseType: 'text'
};

Параметр responseType должен быть установлен на text не только в заголовках, но и в httpOptions. В противном случае Angular будет анализировать ответ на вызов как JSON.

0 голосов
/ 08 мая 2018

Попробуйте изменить ваш запрос следующим образом:

login(){ 
const headers = new HttpHeaders(); 
headers = headers.append('Content-Type': 'text/xml'); 
headers = headers.append('Accept', 'text/xml'); 
let body = '<authenticationDetail>' 
           '<username>Username</username>' 
           '<password>Password</password>' 
           '</authenticationDetail>'; 

return this.http.post('localhost/login.ws',body , { headers: headers, responseType: 'text' })
.subscribe(
        res => {
        parseString(res.text(), (err, result) => {
            if (err) {
                return console.log('invalid XML');
            }

            console.log(result);
        })
    },error => {
            console.log('There was an error: ', error);
        }
    );
}
0 голосов
/ 08 мая 2018

Попробуйте

const postedData = '<authenticationDetail>' +
                   '<userName>myusername</userName>' +
                   '<password>mypassword</password>' +
                   '</authenticationDetail>';

Строка шаблона добавляет пробельные символы "новая строка", попробуйте классический подход с обычными строками.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...