3

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; }
}

enter image description here

4
  • change request type in ajax to 'POST' and change your method To accept POST and change parameter type to 'PlaceDetails' Commented Jan 3, 2017 at 10:00
  • @Dalton I get null value, what to do Commented Jan 3, 2017 at 10:12
  • Modify data: put data:output and remove [FromBody] Commented Jan 3, 2017 at 11:09
  • 2
    @ARUNEdathadan: Also please don't edit the question with things you've tried since the initial question as it may completely change the meaning of the question Commented Jan 3, 2017 at 11:58

1 Answer 1

8

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";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes good answer, but does the hyperbole about being a beginner help him out in anyway? Will it help others to see thinly veiled insults? Does it really make you feel better?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.