0

I am trying to read data from a CosmosDB using an Ajax call and want data in JSON format.

Controller

[ActionName("Status")]
public async Task<ActionResult> Status(string id, string partitionKey)
{
    if (id == null)
    {
        return BadRequest();
    }

    Item item = await _cosmosDbService.GetItemAsync(id, partitionKey);
    if (item == null)
    {
        return NotFound();
    }   
    
    return Json(item);
}

The item has data in the below format:

enter image description here

Javascript

function getStatus(inID, inPKey) {       
    var serviceURL = '/Item/Status';
    var inData = { id: inID, partitionKey: inPKey }

    $.ajax({
        type: "GET",
        url: serviceURL,
        data: inData,            
        dataType: "json",
        success: successFunc,
        error: errorFunc
    });          
}

function successFunc(data, status) {
    alert(data);
}

function errorFunc() {
    alert('error');
}

For some reason I am getting the "data" as error. How do I get the data in a valid json format? Please help.

Arun Vijay
  • 121
  • 1
  • 2
  • 12

1 Answers1

0

Possible duplicate, I think you can find your answer here: https://stackoverflow.com/a/6587249/10374151

Simply, you are not sending correct json data, replace

data: inData, 

With

data: JSON.stringify(inData);

Also you are missing the contentType property, the dataType although sounds misleading is very directly linked to the response you expect, not the data you send.

Declare contentType like this:

contentType: 'application/json; charset=utf-8',

This should solve your problem. The issue should be with your controller being unable to read the data it is receiving, thus you are not going to find a match in your database.

GodLess
  • 48
  • 8