locked
JSON working, but JSON array fails in production RRS feed

  • Question

  • User930584213 posted

    I have created a Web API that I have deployed under these paths (you're welcome to see my certifications data):

    http://stevegaines.info/api/Certifications

    http://stevegaines.info/api/Certifications/1

    http://stevegaines.info/api/Exams

    http://stevegaines.info/api/Exams/1

    The first and third calls fail with the message "An error has occurred."  Those calls work on my local machine when I run the Web API in debug from Visual Studio and I navigate to the local paths.  The look like a typical JSON array in the browser as shown below.  So why do the array objects appear in the same browser from the debug server, but they don't appear in production from my domain name on my web host server?  Do you see them in your browser?  Do you get the same error message?  Web API seems very temperamental.

    [{"CertificationID":1,"CertificationName":"Microsoft Certified Solution Developer","CertificationSubName":"Visual Basic 4.0","CertificationShortName":"MCSD 1997 - Visual Basic 4.0","CertificationAcronym":"MCSD","CertificationOrder":1,"CertificationDate":"1997-05-04T00:00:00","CreatedDate":"2006-05-26T11:46:00"},{"CertificationID":2,"CertificationName":"Microsoft Certified Solution Developer","CertificationSubName":"Visual Basic 6.0","CertificationShortName":"MCSD 1999 - Visual Basic 6.0","CertificationAcronym":"MCSD","CertificationOrder":2,"CertificationDate":"1999-12-05T00:00:00","CreatedDate":"2006-05-26T11:46:00"},{"CertificationID":4,"CertificationName":"Microsoft Certified Application Developer","CertificationSubName":"Visual Basic .NET","CertificationShortName":"MCAD 2006 - Visual Basic .NET","CertificationAcronym":"MCAD","CertificationOrder":3,"CertificationDate":"2006-03-26T00:00:00","CreatedDate":"2006-05-26T11:47:00"},{"CertificationID":3,"CertificationName":"Microsoft Certified Solution Developer","CertificationSubName":"Visual Basic .NET","CertificationShortName":"MCSD 2006 - Visual Basic .NET","CertificationAcronym":"MCSD","CertificationOrder":4,"CertificationDate":"2006-04-29T00:00:00","CreatedDate":"2006-05-26T11:47:00"},{"CertificationID":5,"CertificationName":"Microsoft Certified Solution Developer","CertificationSubName":"Visual C# .NET","CertificationShortName":"MCSD 2006 - Visual C# .NET","CertificationAcronym":"MCSD","CertificationOrder":5,"CertificationDate":"2006-06-29T00:00:00","CreatedDate":"2006-07-03T07:13:00"}]

    Thursday, May 5, 2016 8:39 PM

Answers

All replies

  • User527778624 posted

    Hi,

    Your first link is throwing "An error has occurred." Upon inspection its throwing 500 http status and no other details.

    You can only debug your application. or add try-catch block and throw the error from webapi action method, then you can check the issue with jquery $.get().error()

    Friday, May 6, 2016 6:54 AM
  • User930584213 posted

    Raju

    Thanks for your attempt at helping me.  The try/catch error is a good idea, although I suspected no error is being thrown.  Regardless, I added a try/catch block to the method and I added an Errors table to log any errors.  I uploaded the new version of the Web API and I called the method.  I got the same error in the browser {"Message":"An error has occurred."}, but no error was logged in the Errors table.  I think the problem is that the method works and it returns an array of JSON objects but the browser and AngularJS can't handle that.  I suspect it needs the array to be wrapped in a root JSON object like this { "Exams": [ original objects array ] }.  But I tried that and it didn't work either.  Unless someone has another idea for how to solve this I am going to give up and work on other issues. Maybe I'll use jQuery to bring back the data.  I wanted to demonstrate AngularJS, but I guess I don't know this well enough.

    Steve

    Friday, May 6, 2016 3:44 PM
  • User527778624 posted

    Hi,

    use try-catch block like this:

    try
    { 
     //your code here
    }
    catch{ throw; }
    

    use chrome dev tools to inspect response in network tab.

    or follow this site for global error logging in webapi:

    http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 6, 2016 5:28 PM
  • User36583972 posted

    Hi DallasSteve

    From your description, I suggest you can try the following steps to solve your problem.

    Step 1: Use F12 and check your error message. Based on the erroneous information and solve the problem about Web API. ensure that the Web API work well.

    Step 2: Try to parse array correctly.

    1: how we can parse the json array object to list:

    http://forums.asp.net/t/1865182.aspx?how+we+can+parse+the+json+array+object+to+list

    2: Introduction to AngularJS and WebAPI:

    http://www.codeproject.com/Articles/874264/Introduction-to-AngularJS-and-WebAPI

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 9, 2016 3:04 AM