Hi fellow StackOverflow users.
I want to fetch the string that comes from web api (Asp.net Core)
This is the code for my controller:
[HttpPost("Xml")]
public string Xml()
{
try
{
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
return _xmlBeautifier.Beautify(reader.ReadToEnd());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
throw ex;
}
}
And this is my Angular Code:
onSubmit() {
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'text/xml',
'Content-Type': 'text/xml'
})
};
this.http.post('http://localhost:5000/api/xml/xml', this.xmlForm.controls['XmlData'].value, httpOptions)
.subscribe(res => {
alert('SUCCESS !!');
})
}
What I am doing for now is checking if the string which is XML is fetched correctly. I don't have a code yet to print the parsed XML but I already have the error.
SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse ()
How do I fix the error? I already tested the API using Postman and it worked correctly.
Please see the screenshot below
Update:
Here is the full error message:
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:5000/api/xml/xml", ok: false, …
message: "Unexpected token < in JSON at position 0"
stack: "SyntaxError: Unexpected token < in JSON at position 0↵ at JSON.parse (<anonymous>)↵ at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:19139:51)↵ at ZoneDelegate.invokeTask (http://localhost:4200/poly
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:5000/api/xml/xml"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:5000/api/xml/xml"
And I'm also using the paper-dashboard-angular template from creative-tim
Update:
I uploaded the sample web api and the sample angular code that i'm using
