I have an action for a GET-reqeust:
[HttpGet]
public string Answer([FromBody]string value)
{
CookieHeaderValue cookie = Request.Headers.GetCookies("user").FirstOrDefault();
game.LogUserAnswer(cookie["user"].Value, value);
return String.Format("<strong>{0}</strong>: {1} <br>", cookie["user"].Value, game.UserGuess(value));
}
When I'm trying to pass any data I've always got null. I invoke the action with ajax-call:
$('#btnAnswer').click(function () {
var value = $('#Answer').val();
$.ajax({
type: "GET",
dataType: "json",
url: uri + '/' + 'Answer' + '/' + value,
data: { '': value },
success: function (data) {
$('#resultsBox').append('<strong>' + data + '</strong><br>');
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
}
});
});
In the Fiddler the url is next: GET /api/values/Answer/132312?=132312 HTTP/1.1
I tried to send another request: GET /api/values/Answer/132312 HTTP/1.1
but still in my action value is null.
Also I've changed public string Answer(string value) (without FromBody), in this case application can't find needful HTTP
.
Could you please tell, what I'm missing?
*Update 1 * var uri = 'api/values';
config.Routes.MapHttpRoute(
name: "apianswer",
routeTemplate: "Home/api/values/answer/{id}",
defaults: new { action = "Answer", id = RouteParameter.Optional }
);
Get requests does not have body ! You should send your parameter as part of your route
[HttpGet]
[Route("Home/api/values/answer/{value}"]
public string Answer(string value = null //in case you want to call answer without any value provided)
{...}
$.ajax({
type: "GET",
url: uri + '/' + 'Answer' + '/' + value,
...
});
or as a query string
[HttpGet]
public string Answer(string value)
{...}
$.ajax({
type: "GET",
url: uri + '/' + 'Answer',
data: {value: value}
...
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.