MVC returns not valid Json result

Hello everybody, i`am facing a problem. 
I have this method

[HttpPost]
        public JsonResult GetCardsOnScanner()
        {
            var service = _container.Resolve<IScannerTasksService>();
            var pcName = "WORK-PC";
            var result = service.GetReadDataByPcName(pcName, true);
            var cardsService = _container.Resolve<ICardsService>();
            var data = cardsService.GetByRFID(result);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

And have this JavaScript method:

GetDataFromScanner : function() {
    var jsonUrl = rootPath + "admin/cards/GetCardsOnScanner/";
    $.ajax({
        type: 'POST',
        url: jsonUrl,
        dataType: 'json',
        success: function(model) {
            var data = model.FoundCards;
            var idsCount = data.length;
            if (idsCount == 0)
                failCountEmptyObject++;
            else {
                for (var i = 0; i < idsCount; i++) {
                    MoveToData.CardIds.push(data[i].Id);
                    Sheets.AddCardToTable(data[i]);
                }
                continueGet = false;
            }
        },
        complete: function () {
            if (continueGet == false) {
                return;
            }
            if (failCountEmptyObject > 15) {
                $('#NotFound').show();
                continueGet = false;
                return;
            } else {
                var time = 1000;
                setTimeout(Sheets.GetDataFromScanner, time);
            }
        },
        error: function(error) {
            console.log(error);
        }
    });
    
};

Script receives response code 200 but still throw an error. After some time i recognize that .NET method return not valid Json. Any ideas whats wrong ? 
Project is on .NET 4.5
 

Daniil T.

var data = cardsService.GetByRFID(result);
            return Json(data, JsonRequestBehavior.AllowGet);

What kind of object is data ? 

And please make a try/catch inside 

public class CardInfo
    {
        public IEnumerable<Card> FoundCards { get; set; }
        public string[] NotFoundCards { get; set; }
    }
public CardInfo GetByRFID(IEnumerable<string> rfids)
        {
            var data = _repository.All().Include(x => x.Client).Include(x => x.CurrentCardAddress).Where(x => rfids.Contains(x.Code)).ToList();
            var result = new CardInfo()
            {
                FoundCards = data,
                NotFoundCards = rfids.Where(x => !data.Any(c => c.Code.Equals(x))).ToArray()
            };
            return result;
        }

Any ideas ? 

Try this

Daniil T.

[HttpPost]
        public JsonResult GetCardsOnScanner()
        {

remove post flag

        public JsonResult GetCardsOnScanner()
        {


Daniil T.

 $.ajax({
        type: 'POST',
 $.ajax({
        type: 'GET',

I can`t send it thru GET method. There is some sensitive data

thenwhy you have setted this..

 return Json(data, JsonRequestBehavior.AllowGet);

also can you provide the  string which is returning the json above,  jut to check the format , or if you can’t  please copy the object and check it in the

http://www.jsoneditoronline.org/

if you have problems with he returned json (which is the case)  this site will show you where it is.

BR

Petar

Thank you for your reply.  

 return Json(data, JsonRequestBehavior.AllowGet);

doesn`t change anything. The problem is that format is not right. It`s not valid. I already checked it in similar websites. It showd that there is error in format. There is not brackets in the beging of the Json. 
Here is the result:

SUPPORT-PC{
    "FoundCards": [
        {
            "Code": "0201410141335340",
            "Status": 2,
            "IsDuplicate": false,
            "ClientId": "d66c0237-d731-4119-afe4-2a4be4eb0c94",
            "CardIndex": 1,
            "Client": {
                "Code": "137325197",
                "FirstName": "*****AS",
                "LastName": "*****AS",
                "Address": "Viln*****",
                "IsAdult": true,
                "Phone": "",
                "PhoneEx": "",
                "Status": 1,
                "Place": 1,
                "Name": "*****AS *****AS",
                "WorkingPlace": 1,
                "ClientStatus": 1,
                "District": "118",
                "ExternalAddress": "",
                "BirthDate": null,
                "City": null,
                "Id": "d66c0237-d731-4119-afe4-2a4be4eb0c94"
            },
            "CardAddressId": "611dc0d5-7007-4322-b30c-593dba8aecef",
            "CardAddress": {
                "Code": "1B1",
                "AddressType": 1,
                "Address": "1B-1",
                "Row": "1",
                "Section": "B",
                "Floor": "1",
                "ConsultingRoom": null,
                "Order": 10,
                "Place": 2,
                "WorkingPlace": 2,
                "AddressTypeEnum": 1,
                "CardIndex": 2,
                "CardIndexEnum": 2,
                "Id": "611dc0d5-7007-4322-b30c-593dba8aecef"
            },
            "CurrentCardAddressId": "611dc0d5-7007-4322-b30c-593dba8aecef",
            "CurrentCardAddress": {
                "Code": "1B1",
                "AddressType": 1,
                "Address": "1B-1",
                "Row": "1",
                "Section": "B",
                "Floor": "1",
                "ConsultingRoom": null,
                "Order": 10,
                "Place": 2,
                "WorkingPlace": 2,
                "AddressTypeEnum": 1,
                "CardIndex": 2,
                "CardIndexEnum": 2,
                "Id": "611dc0d5-7007-4322-b30c-593dba8aecef"
            },
            "CardIndexEnum": 1,
            "CardStatus": 2,
            "Tasks": [
                
            ],
            "Name": "Suaugusio bendra: 0201410141335340",
            "Id": "b25281e1-94ed-47a5-8fb2-8a3b852284cf"
        }
    ],
    "NotFoundCards": [
        "1111222211112222"
    ]
}

I know where is error. But i can`t figure out what causes that error. 

Leave a Reply