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.

4
  • 1
    Can you post the get method in the controller. Commented Sep 2, 2015 at 17:39
  • 1
    @mattfetz more importantly an example of the JSON response to the get method Commented Sep 2, 2015 at 17:40
  • Ok now go to your web browsers web tools and check the network response from this ajax call. You will be able to see the entire JSON response here and determine if you are getting the right data. Commented Sep 2, 2015 at 17:54
  • any ideas why this is happening? Commented Sep 2, 2015 at 19:13

2 Answers 2

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);
}  
Sign up to request clarification or add additional context in comments.

3 Comments

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
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
I attempted your suggestions without success, I updated my original post to reflect what I tried.
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.

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.