'
I'm studying ASP.NET Core and Angular.
I already created the API (ASP.NET Core). And I was able to call that api using Angular.
This is the controller
[Route("api/[controller]")]
[ApiController]
public class XmlBeautifierController : ControllerBase
{
private readonly IXmlBeautifier _xmlBeautifier;
public XmlBeautifierController(IXmlBeautifier xmlBeautifier)
{
_xmlBeautifier = xmlBeautifier;
}
[HttpPost("XmlBeautifier")]
public string XmlBeautifier([FromBody] XmlData data)
{
try
{
Console.WriteLine(data);
Console.WriteLine("Parsed XML Data: " + _xmlBeautifier.Beautify(data.Xml));
//read the content
return _xmlBeautifier.Beautify(data.Xml);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
throw ex;
}
}
}
I checked that the api is called properly. But when I want to read the result of the controller. The problem comes in. This is the error which I don't understand
SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad
I've read some tutorials and it says that in order to read the result is by something like this
onSubmit() {
// TODO: Use EventEmitter with form value
console.warn(this.xmlForm.value);
this.http.post('http://localhost:5000/api/XmlBeautifier/XmlBeautifier', { Xml: this.xmlForm.controls['XmlData'].value })
.subscribe(res => {
console.log(res);
alert('SUCCESS !!');
})
}
What is wrong with my code?