Reputation: 10135
This is Google API - Javascript code
var output = new Object();
output.PlaceID = place.place_id;
output.Longitude = place.geometry.location.lng();
output.Latitude = place.geometry.location.lat();
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
url: 'api/StorePlaces/Get',
type: 'POST',
data: { "value":output },
success: function (result) {
// Do something with the result
}
});
How to receive at the controller
// GET api/values/5
[HttpPost("{PlaceDetails}")]
public string Get(PlaceDetails value)
{
return "value";
}
In this case I get null value
I was unable to send the string , if I can send the Object as such, it is better.
This can be used as receiving object
public class PlaceDetails
{
public string PlaceID { get; set; }
public string Longitude { get; set; }
public string Latitude { get; set; }
}
Upvotes: 3
Views: 13123
Reputation: 64288
There are multiple things wrong with your code, maybe consult a few beginner tutorials first?
First, you have to look at the object your are sending, it's very obvious!
You are sending
{
"value" : {
"PlaceID" : "",
"Longitude " : "",
"Latitude " : ""
}
}
Where the expected answer is
{
"PlaceID" : "",
"Longitude " : "",
"Latitude " : ""
}
So you have to use this in JavaScript:
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
url: 'api/StorePlaces/Get',
type: 'POST',
// do not wrap it in object with value property here!!!!
data: JSON.stringify(output),
success: function (result) {
// Do something with the result
}
});
Second, your Controller action (why the hell is it called Get
when it's a post request?)... the [HttPost("{PlaceDetails}")]
attribute is plain wrong.
This would expect a PlaceDetails
parameter in the route. You don't such one! Just remove it. Also, the [FromBody]
attribute is missing to tell it to deserialize the model from the http request body
[HttpPost]
public string Get([FromBody]PlaceDetails value)
{
return "value";
}
Upvotes: 8