I am trying to post JSON data to the controller. The post is successful but the data is null.
This is the ajax code:
$(document).ready(function () {
var result = [];
$.ajax({
type: 'GET',
url: 'https://api.ipdata.co/49.206.6.229?api-key=accesskey',
success: function (data) {
console.log(data.city),
console.log(data.latitude),
console.log(data.longitude),
result= { "city": data.city, "latitude":data.latitude, "longitude":data.longitude };
debugger
$.ajax({
contentType: 'application/json; charset=utf-8',
datatype: 'JSON',
type: 'POST',
url: '/GeoLocation/GeoLocationData',
data: result ,
success: function (data) {
console.log(data),
alert('Post Successful');
},
error: function (data) {
alert('error');
}
});
debugger
}
});
});
I am getting the Post Successful alert, but am not able to use the data in the controller.
This is my controller Action :
[HttpPost]
public ActionResult GeoLocationData(Location location)
{
var city = location.city;
var lat = location.latitude;
var lon = location.longitude;
return Ok();
}
My Model:
public class Location
{
public String city { get; set; }
[Column("latitude")]
public double? latitude { get; set; }
[Column("longitude")]
public double? longitude { get; set; }
}
This is the JSON data I am getting from the api:
data: { ...
city: "xxxx",
continent_code: "xx" ,
latitude: 17.3753,
longitude: 78.4744... }
When I inspect the debugger, I can see that the data is being passed correctly, however in the second ajax call though I have given
data : result
, data takes the
JSON response value
. Why is this so?