A
A
Andrey Romanyuk2018-05-15 17:58:58
JavaScript
Andrey Romanyuk, 2018-05-15 17:58:58

Using return Json() method on client in success function(data) undefined. How to fix?

Project on asp.net core, web api 2.
Method:

There are approximately 600 products in the database. Linq selection pulls out goods, checked it in the debugger.

[HttpPost("ConfectionsJsonResult")]
        public JsonResult ConfectionsJsonResult()
        {
            IEnumerable<Products> confections = DbNorthWind.Products.Where(p => p.CategoryId == 3);

            return Json(confections);
        }

Customer. Result (in comments):

Property in Json object is correct ( ProductName as column name in database ).
$.getJSON({
        type: 'POST',
        dataType: 'json',
        contentType: 'application/JSON',
        url: '/api/Product/ConfectionsJsonResult',
        success: function (data) {

            var results = $("#ConfectionsList");

            alert(data); //[object Object],[object Object],[object Object],[object Object],[object Object],...
            alert(data[0].ProductName); //undefined

-------------------------------------------------- -------------------------------------------------- -------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
------ -------------------------------------------------- -------------------------------------------------- -------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is how the working code looks , but I was asked to remove "var JsonConfections = JsonConvert.SerializeObject(confections);" as it is a system load.
[HttpPost("ConfectionsJsonResult")]
        public JsonResult ConfectionsJsonResult()
        {

            var confections = DbNorthWind.Products.Where(p => p.CategoryId == 3);

            var JsonConfections = JsonConvert.SerializeObject(confections);

            return Json(JsonConfections );
        }

On the client, jQuery.parseJSON(data) in the success function still works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2018-05-15
@BLek2

Make a request to your api in the browser line or via postman. If a normal json arrives, then the matter is in the ajax request, in its settings, if not, then the object is not serialized in json.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question