Making Post request with XML data in Angular 5

主宰稳场 提交于 2019-12-13 13:53:17

问题


I'm am new to angular and using angular 5.

I'm making a http post request and I want to send xml data to backend as my backend only accept data in xml format.

I've followed some tutorials but couldn't get it to work. Please let me know what I'm doing wrong and how can I make it work.

This is how I'm making request.

signin(){
    const headers = new HttpHeaders({responseType: 'text' , 'Content-Type': 'text/xml' }).set('Accept', 'text/xml');
    let body =  '<request>' 
                '<username>Username</username>' 
                '<password>Password</password>' 
                '</request>';
    const hdr = {headers:headers , body:body};
    console.log("Checking:  " , hdr);
    return this.http.post('https://66.128.132.126:8002/?event=account_login' , hdr);
}

Here is my console for this:

And here is my network:


回答1:


By default Angular CLI made a Preflight Request in order to check CORS: https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

It seems that you backend isn't handling this request correctly. We need backend info to help you




回答2:


the responseType and body should not be appended with headers. body should be sent separetly and with headers and responseType options object should be formed and sent as a parameter to post.

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

    return this.http.post('https://66.128.132.126:8002/?event=account_login',body , { headers: headers, responseType: 'text' });
}


来源:https://stackoverflow.com/questions/49125638/making-post-request-with-xml-data-in-angular-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!