0

this is simple but it dosent work

in angularJS part i have this code :

 var data={"ID":1,"Key":"********"} $http.post("/Home/DeleteListItem", data) .success(function (data) { alert(JSON.parse(data)); }).error(function (response) { alert(response); }); 

and C# Part is like this

 [HttpPost] public JsonResult DeleteListItem(Entity entity) { kishAppEntities db = new kishAppEntities(); Stream req = Request.InputStream; db.Configuration.ProxyCreationEnabled = false; var data = //some delete query return new JsonResult() { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } public class Entity { int ID { set; get; } string Key { set; get; } } 

there is no data in entity i dont have clue aboute it???? enter image description here

i use this post method as second approach but still dosent work

 var entity = { "ID": 1, "Key": "********" } $http({ url: "./general/DeleteListItem", method: "POST", data: JSON.stringify({ entity: entity }), headers: { 'Content-Type': 'application/json' } }).success(function(data, status, headers, config) { }).error(function(data, status, headers, config) { }); 
4
  • What is the error you are getting? Based upon the provided code, I can say that angular promise usage is wrong. use $http.post("/Home/DeleteListItem", data).then(function(successResponse){}, function(errorResponse) {});
    – Kalyan
    CommentedDec 16, 2016 at 0:49
  • i dont have a error. problem is i dont get my post data from clientCommentedDec 16, 2016 at 0:50
  • please see updateCommentedDec 16, 2016 at 0:56
  • Please check my answer below. using .success is not a right way in asynchronous call. refer: codelord.net/2015/05/25/dont-use-$https-success
    – Kalyan
    CommentedDec 16, 2016 at 1:02

3 Answers 3

1

Your ID and Key properties are not accessible. Put before your properties public access modifier like this:

public class Entity { public int ID { set; get; } public string Key { set; get; } } 
0
    0

    try this

    $http.post("/Home/DeleteListItem", data) .then(function(successResponse){ //Your code to handle response }, function(errorResponse) { //your code to handle error }); 
    6
    • Is that returns any error? Also, check if the call hitting your MVC controller method. Note you won't get the result immediately.
      – Kalyan
      CommentedDec 16, 2016 at 1:06
    • it call mvc contoller and return whit no errore if you see the photo i debug my controller so it call the method it just not have my payload dataCommentedDec 16, 2016 at 1:10
    • sorry.. my network blocks image loading. Please use the [FromBody] attribute on your controller parameter : public JsonResult DeleteListItem([Frombody] Entity entity) {
      – Kalyan
      CommentedDec 16, 2016 at 1:21
    • vs ide dos note recognized [Frombody]CommentedDec 16, 2016 at 1:24
    • Sorry, there is a typo. it should be [FromBody]
      – Kalyan
      CommentedDec 16, 2016 at 1:28
    0

    Try using jquery $.param(data). You can also try angular $httpParamSerializer https://code.angularjs.org/1.4.0/docs/api/ng/service/$httpParamSerializer

    0

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.