Lamloumi Afif Lamloumi Afif - 5 months ago 22
Ajax Question

From body null value

I made this ajax call

var value = $("#newval").val();
$.ajax({
type: "Put",
url: "/api/updateparameter/" + global_id,
data: value,
success: function () { HidePopUp(); }
});


to this service :

[HttpPut]
[Authorize(Roles = "superadmin,admin")]
[Route("updateparameter/{id}")]
public IHttpActionResult UpdateConfiguration(int id, [FromBody] string value)
{
ajt_configuration obj = repoConfiguration.FindById(id);
if (obj == null) return Ok();
obj.value = value.ToString();
repoConfiguration.UpdateConfiguration(obj.key, obj.value);
return Ok();
}


The problem is that
value
is always null in the service !!

So I need to know :


  1. What it takes null as value?

  2. How can I fix this?


hsh hsh
Answer

You didn't send the object to your service properly, you should define an object of key/value pairs that contains name and value of your variables.so you should change your ajax request like this:

var value = $("#newval").val(); 
$.ajax({
    type: "Put",
    contentType: "application/json; charset=utf-8",
    url: "/api/updateparameter/" + global_id,
    data: {'value': value},
    success: function () { HidePopUp(); }
});