In my Project, this is the JavaScript code:
function RetrieveData() {
debugger;
var url = 'http://localhost:50951/api/Home';
$.ajax({
type: "GET",
url: url,
dataType: "json",
success: function (data) {
alert("Retrieve Successful");
},
error: function (error) {
alert("Error Retrieving Data");
}
});
}
This is the corresponding WebApi code:
public class HomeController : ApiController
{
public IEnumerable<KeyValuePair<string, string>> Index()
{
List<KeyValuePair<string, string>> listString = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string> ("India","India"),
new KeyValuePair<string, string> ("Australia","Australia"),
};
return listString;
}
}
When trying to call the RetrieveData
function, it triggers the API but shows an error message in JavaScript. It does not enter the success block. Is there a way to receive the list data in JSON response when using the API?