Using JSON to access external XML feed.
m
I am converting a U7 site to U10 and part of it involves getting some content from an XML feed. Current set up involves ajax to an API Controller to a Logic file which access the XML. I have reproduced this more or less the same. I can see that the actual content is populating my C# list but between there and the JavaScript it's turning into Object object, undefined. This works in u7, just wondering what I'm missing. Thanks
Copy code
js
function LoadInvestorNews() {
    $.ajax({
        url: '/Umbraco/Api/InvestorNewsApi/GetInvestorNews',
        async: false,
        type: "GET",
        data: {},
        dataType: "json",
        contentType: "application/json",
        error: function (data) {
            console.log("error loading news" + data);
        },
        success: function (data) {
            console.log("success loading news", data);
            for (i = 0; i < data.length; i++) {
                var html = '<li>' +
                    '<span class="date"><a href="' + data[i].LinkUrl + '" target="_blank">' + data[i].Date + '</a></span> - ' +
                    '<span class="headline"><a href="' + data[i].LinkUrl + '" target="_blank">' + data[i].Header + '</a></span>';

                html += '</li>';
                console.log(html);
                $("#investor-news-list").append(
                    html
                );
            };
        }
    });
}
Main C# code:
Copy code
cs
public static  List<InvestorNewsJsonItem> GetInvestorNewsJson()
        {
            List<InvestorNewsJsonItem> itemsList = new List<InvestorNewsJsonItem>();
            XDocument xml = GetXml("https://otp.tools.company.com/clients/uk/client/rns/xml-feed.aspx?culture=en-GB");
            
            if (xml != null)
            {                
                try
                {
                    itemsList = xml.Root.Descendants("RNSSummaries").Elements("RNSSummary").Select(node => new InvestorNewsJsonItem
                    {
                        Header = node.Element("Title").Value,
                        Date = DateTime.Parse(node.Element("pubDate").Value).ToString("dd MMM yyyy"),
                        LinkUrl = node.Element("ShareURL").Value,
                    })
                .ToList();
                    foreach (var item in itemsList)
                    {
                        Console.WriteLine($"Header: {item.Header}");                        
                    }
                }
                catch (Exception ex)
                {
                     Console.WriteLine(ex.Message);
                     Console.WriteLine(ex.StackTrace);
                }
            }
           
            return itemsList;
        }
    }
I'm using an UmbracoApiController. Should I be using something else?
Copy code
cs
public class InvestorNewsApiController : UmbracoApiController
    {
        public List<InvestorNewsJsonItem> GetInvestorNews()
        {
            return InvestorNewsLogic.GetInvestorNewsJson();
            
        }
        
    }
r
If you load the api url in its own tab, does it return anything? Have you checked the network logs in your browser's dev tools to see what the response is there? Also you can get some nicer formatting/highlighting on the code blocks with three backticks, see here:
m
Thanks for that re the formatting - makes a big difference 🙂
If I load the api url in its own tab I get the correct content.
I'm not getting anything in Network - wondering would it be a CORS issue, this is running locally right now, will put it up on a server.
Actually - it's working now - for some reason I now was getting content in the Javascript console and noticed that there was a difference in case - data[i].Date should be data[i].date. Still don't know why I was only getting Object object before but beyond caring now. Thanks for your help!
r
No problem, glad I could help diagnose at least :)
3 Views