2

I have a api controller function that returns an array of objects. The following is how I call my function and an example of what happens:

$.getJSON("/api/mycontroller/myfunc/?id=" + id, function (data) {
   data[0].field1  <- correct value here
   data[0].field2  <- correct value here

   data[1].field1  <- null
   data[1].field2  <- null
}

The first element contains the correct values as illustrated but all other elements are null. data.length returns the correct size of the array and I have verified that my controller is returning the values I anticipate. Am I missing something here???

UPDATE: Here is my controller method:

public data[] myfunc(long id)
{
     return db.MyTable.Where(x => x.ID == id).ToArray();
}  

As mentioned I have verified that the array being returned contains the correct values.

JSON response is this:

Object { $id="1",  Referral={...},  MeetingID=1,  more...}
$id     "1"
Referral    Object { $id="2",  IntakeMember={...},  Meetings=[3],  more...}
MeetingID   1
MeetingDate "2015-01-01T00:00:00"
Notes   "test1"
ReferralID  22

Object { $ref="16"}
$ref    "16"

Object { $ref="17"}
$ref    "17"

So as you can see elements 2 and 3 just have this "ref" value in them...not sure why??

UPDATE #2:

Tried $.get rather than $.getJSON without success, same issue. I also tried making the following changes to the controller:

MyData[] data = db.MyTable.Where(x => x.ID == id).ToArray();
JsonResult jsonResult = new JsonResult
{
    Data = data,
    JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
return jsonResult.Data;

I still experience the same issue.

mgrenier
  • 1,409
  • 3
  • 21
  • 45

2 Answers2

1

$.getJSON() expects the returned data to be JSON. Change your controller method to return JSON

public JsonResult myfunc(long id)
{
     var data = db.MyTable.Where(x => x.ID == id);
     return Json(data, JsonRequestBehavior.AllowGet);
}  
  • it appears Json is a member of the Controller library, however my function is in an ApiController...can I still use it? I get a not recognized error – mgrenier Sep 03 '15 at 12:32
  • Dammit - just realized its WebAPI. I guessing what your returning is XML, not JSON, so you can first try changing `$.getJSON()` to just `$.get()`. Alternatively you can look at the answers [here](http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome) –  Sep 03 '15 at 12:38
  • I attempted your suggestions without success, I updated my original post to reflect what I tried. – mgrenier Sep 03 '15 at 12:56
1

For some reason my problem got resolved when I created a view in my database and used that to return data rather than directly querying the table it resides in. I put this hear in case anyone experiences an issue similar to mine but if anyone who know why this happened I would be interested to hear...please comment on this answer.

mgrenier
  • 1,409
  • 3
  • 21
  • 45
  • I had a similar problem but fixed it by manually looping through my LINQ to Entities resultset and creating new objects. I do not know what the problem was, either, but I wasn't about to alter my database schema for it... – Matt Mar 15 '17 at 18:57