1

I am having issues with finding the correct way to send an array of objects to my API using AngularJS.

FrontEnd Code

     function getPrices(articles) {
            return $http.get('http://someurl/api/prices/getprices', { params: { articles: articles } }).then(function (res) {
                   // do something with prices
                }, function (err) {
                    // handle error
                });
      }

Articles are of the type

var oneArticle = {
  code: 'someCode',
  quantity: 1,
  stockUnit: 'piece'
}

Api code

[VersionedRoute("getprices")]
[HttpGet]
public IHttpActionResult GetPrices([FromUri]List<Article> articles) {
 // do something with input
}

Article class

public class Article {
  public string Code {get;set;}
  public int Quantity {get;set;}
  public string StockUnit {get;set;}
}

Some questions:

1) Why do I not receive any data on my API. Articles is always null

2) Is this the right approach?

Thanks

EDIT 1: Using a post option I receive following data in my request but I still don't know how to handle it on the API.

enter image description here

5
  • try return $http.get('http://someurl/api/prices/getprices', articles }) articles is a list Commented Oct 24, 2017 at 8:06
  • you want to send the same data - articles you're returning in the end or you just want to get the data based on a filter or something? Commented Oct 24, 2017 at 8:36
  • @Naimad : This is not an option. Commented Oct 24, 2017 at 9:02
  • @Tomo : I just want to send the results to the server but I always receive NULL as input. Commented Oct 24, 2017 at 9:03
  • Your articles should be in the query parameter. I'm not familiarise with the API that you are using but should be a method to get the query params. If you can get the request in your method, you will see the articles in the query and you will need to deserialise them. Commented Oct 24, 2017 at 9:38

3 Answers 3

1

I finally got it working.

@Tomo: Thanks for the effort

@Naimad: My apologies, you were right from the beginning.

Here is the working solution:

Frontend:

function getPrices(articles) {
            return $http.get('http://someurl/api/prices/getprices', articles).then(function (res) {
                   // do something with prices
                }, function (err) {
                    // handle error
                });
      }

Backend

[VersionedRoute("getprices")]
[HttpPost]
public IHttpActionResult GetPrices([FromBody]List<Article> articles) {
 // do something with code
}
Sign up to request clarification or add additional context in comments.

Comments

0
.controller('LoginController', ['$scope', '$http', function ($scope, $http) {
    function getPrices(articles) {
        $http.get('http://someurl/api/prices/getprices')
        .success(function (data) {
            articles: data
        }
    }
}])

5 Comments

.success is outdated, one should use something like: .then(function successCallback(response) { }; }, function errorCallback(error) { });
.controller('LoginController', ['$scope', '$http', function ($scope, $http) { function getPrices(articles) { $http.get('someurl/api/prices/getprices') .then(function (respose) { articles: response.data } } }])
This is not an answer to my question. I did not include the complete code into this post but offcourse I know to put my code into a controller/service/...
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
If you want an answer, provide more info. Mine was a comment and comments don't need tobe answers. I think you don't know what you want to do or at least you're not explaining it in a right manner.
0

Have u tried

return $http({
            url: '/api/SomeCtrl/GetPrices',
            method: 'POST',
            data: JSON.stringify({ Article : articles }),
            headers: { 'Content-Type': 'application/json' }
        });

and

public IHttpActionResult GetPrices([FromUri]Article articles) {

or

[HttpPost]
public void GetPrices(Article articles)

where instead of void you put whatever you are returning ?

1 Comment

so your C# model is empty? you're sending a string? how does your C# method looks like now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.