Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jan Arenö 9 posts 40 karma points
    Apr 02, 2015 @ 14:04
    Jan Arenö
    0

    UmbracoAuthorizedJsonController returning empty result

    I have a controller with a GetById function

            public Product GetById(int id )
            {

                var query = new Sql().Select("*").From("jf_product").Where<Product>(x => x.Id == id);
                var p = DatabaseContext.Database.Fetch<Product>(query).FirstOrDefault();

                return p;
            }

    From the backend I call this and the url is correct http://localhost:56682/umbraco/backoffice/JfpAddon/ProductApi/GetById?id=13

    I also can hit a debug marker in GetById and can see that variable p is populated and returned. Yet I get a faulty result in response. The result from the request is just:

    )]}',
    {}

     

    I found a tip that I could test to use the UmbracoAuthorizedApiController instead, but then the result is just {}

    Somewhere the Json serializer don't work as expected. The Product object returned is a poco object.

        [TableName("jf_product")]
        [DataContract(Name = "product")]
        public class Product
        {
            public Product() { }

            [PrimaryKeyColumn(AutoIncrement = true)]
            public int Id { get; set; }

            public string ItemNo { get; set; }
            public string Name { get; set; }
            public string UnitOfMeasure { get; set; }
            public bool IsInStock { get; set; }
            public int SortOrder { get; set; }
            public bool SoldOutWeb { get; set; }
        }

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Apr 03, 2015 @ 16:25
    Jan Skovgaard
    0

    Hi Jan

    How many results did you expect to get returned? Could there be a query done against the wrong table for instance?

    /Jan

  • Jan Arenö 9 posts 40 karma points
    Apr 13, 2015 @ 11:13
    Jan Arenö
    0

    Sorry for late reply, but I expected umbraco to convert my Product class to json format. I have a debug breakpoint and see that var p is the correct post, but the result when using firebug says that response from http://localhost:56682/umbraco/backoffice/JfpAddon/ProductApi/GetById?id=21 is just

    )]}', {}
  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 13, 2015 @ 11:28
    Dave Woestenborghs
    0

    Just a wild guess. But I had a problem once with a property called name. Maybe giving it another name for json using the datacontract attribute will work.

    But like i said it is a wild guess.

    Dave

  • Anthony Barsotti 26 posts 66 karma points
    Jun 11, 2015 @ 20:34
    Anthony Barsotti
    0

    I'm getting those )]}', characters at the beginning of my response too, any idea what that is?

  • Pau 14 posts 75 karma points
    Jun 25, 2015 @ 11:44
    Pau
    0

    Hey,

    If you use UmbracoAuthorizeApiController you need to add [Umbraco.Web.WebApi.UmbracoAuthorize] before your method.

    Like this:

    [Umbraco.Web.WebApi.UmbracoAuthorize]
    public Product GetById(int id )
            {
    
                var query = new Sql().Select("*").From("jf_product").Where<Product>(x => x.Id == id);
                var p = DatabaseContext.Database.Fetch<Product>(query).FirstOrDefault();
    
                return p;
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft