-1

Team - I am new to jquery and facing issue with a valid json response giving unknown error.

Web based C# MVC and backend database is SQL Server. I am using EF.

If I use the below code:

IQueryable dbResult = dbContext.ParentRecord.Where(row => row.Id ==id).Include(row => row.ChildRecord);

if (dbResult != null) { return Ok(dbResult) }

I have verified that the HTTP Status code is 200, the child records are populated correctly and tested the structure to be a valid json.

However the above encounters error in get function. If I remove the Include - the ParentRecord.ChildRecord is null and the get works.

There are no details available about the error in the xhr object. It enters the error function and response is undefined. (code below)

Please note: I have tried with fail function, datatype: "json", content-type: "application/json, charset=UTF-8" combinations as well.

$.ajax({
url: _url,
cache: false,
error: function (xhr, status, error) {
alert("error : " + xhr.responseText);
}
}).done(function (data) {

alert("inside " + data);
})

Can anyone point me to the root cause of the error? Or help me get more details on the actual error?

Cadenza
  • 1
  • 3
  • include `success` for e.g: `cache: false, success: function(reponse){ //parse your serverside code from resonse }`. I hope this works for you – ARr0w Apr 19 '18 at 06:07

2 Answers2

0

include success for e.g:

cache: false, 
success: function(reponse){
//parse your serverside code from resonse 
}

I hope this works for you.

plus in javascript/jquery, execution stops at the line where it finds an error in code. and in your ajax you have a extra 'comma' at the end. I'll Bold it.

.ajax({
url: _url,
cache: false,
error: function (xhr, status, error) {
alert("error : " + xhr.responseText);
}, // <=== there's extra comma here. When comma is mentioned it expects for another parameter as you're passing an object. Remove it. 
}).done(function (data) {

alert("inside " + data);
})
ARr0w
  • 1,701
  • 15
  • 31
  • Thanks for pointing out. The comma is a typo in the post. Please ignore. Removed comma from the post. I have already tried with success combination as well. – Cadenza Apr 19 '18 at 06:30
  • @Cadenza if you are saying you are receiving 200 code and sure that you are sending over some data and it is sent. Then you have to debug the 'success' function. add `debugger;` inside the function of success and whenever this ajax is called just before that press `F12` on browser to debug and see what you getting.. – ARr0w Apr 19 '18 at 06:35
  • it is not entering the success function. success: function (data) { console.log(data); } – Cadenza Apr 19 '18 at 07:36
  • hmm call `$.(ajax){()}` ? its not even giving errors at any line? – ARr0w Apr 19 '18 at 07:38
  • https://stackoverflow.com/questions/4508409/ajax-method-call @Cadenza see if this helps – ARr0w Apr 19 '18 at 07:39
  • it enters the error function as depicted in the code above. However there are no details of the error. Hence getting as undefined. – Cadenza Apr 19 '18 at 08:50
  • sort out why it is getting in error. It means status is not 200. :) your code is breaking on serverside. – ARr0w Apr 19 '18 at 09:03
  • as mentioned - status code is 200 and the response json is valid. That is the reason why I posted this issue in this forum :) – Cadenza Apr 19 '18 at 09:20
  • change `IQueryable dbResult` to `var dbResult` and then `return Ok(dbResult.ToList());` – ARr0w Apr 19 '18 at 09:22
  • Tried - same results :( – Cadenza Apr 19 '18 at 09:32
  • if you could create a sample fiddle with the data you're getting, c# and js. Then we might able to code out the issue. – ARr0w Apr 19 '18 at 09:47
0

The issue is traced to Fluent API in C#. Apparently what I did not realize is that jquery is not only expecting a valid json structure but also validating the json response with the respective class.

Unfortunately jquery get error did not provide me any details about the error, which made it hard to detect the anomaly (esp. for newbies like me)

The validation to class failed in this case as it was expecting a ChildRecord.ParentRecord = null field in the response structure. This anomaly is due to Fluent API navigation property definition where the ChildRecord class has the following declaration:

[ForeignKey("PID")]
public virtual ParentRecord ParentRecord { get; set; }

The work around would be to either explicitly set ChildRecord.ParentRecord as null and ensure it is passed into the response or change the Fluent API definition.

I went with the second option.

Cadenza
  • 1
  • 3