I'm getting a null parameter for "id". If I don't JSON.Stringify the id I get the JSON primitive error.
$scope.deleteSite = function (id) {
$http.post(ROOT + 'SiteList/Delete/', JSON.stringify(id)) //null id
//$http.post(ROOT + 'SiteList/Delete/', id) //Invalid JSON primitive: 5f6d794f-bf13-4480-9afd-3b10d7b6ae32.
.success(function (result) {
// log to console?
}).
error(function (data, status, headers, config) {
// log to console?
});
Here is my siteList delete controller.
public JsonResult Delete(String id)
{
try
{
var convertedID = new Guid(id);
_siteService.Delete(convertedID);
return Json("OK", JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json("Error" + e.Message);
}
}
A note, you can use angular.toJson
as well. Which I think could be better - but you can look at the implementation differences if you wish.
Is this Web Api?
Web Api:
You have to configure the params, because delete works this way.
$scope.deleteSite = function (id) {
$http.delete(ROOT + 'SiteList', { params: { id: id }})
.success(function (result) {
// log to console?
}).
error(function (data, status, headers, config) {
// log to console?
});
}
Controller Action, [FromUri]
is needed because you configure the params:
public JsonResult Delete([FromUri] Guid id)
{
try
{
_siteService.Delete(id);
return Json("OK", JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json("Error" + e.Message);
}
}
Unconfigured params means:
$scope.deleteSite = function (id) {
$http.delete(ROOT + 'SiteList/' + id )
.success(function (result) {
// log to console?
}).
error(function (data, status, headers, config) {
// log to console?
});
}
Controller Action:
public JsonResult Delete(Guid id)
{
try
{
_siteService.Delete(id);
return Json("OK", JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json("Error" + e.Message);
}
}
If this isn't Web Api, then you just need to make an object out of the request data.
MVC :
$scope.deleteSite = function (id) {
$http.post(ROOT + 'SiteList/Delete/', { id: id})
.success(function (result) {
// log to console?
}).
error(function (data, status, headers, config) {
// log to console?
});
}
Controller Action:
public JsonResult Delete(Guid id)
{
try
{
_siteService.Delete(id);
return Json("OK", JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json("Error" + e.Message);
}
}
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.