Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of Objects being sent as one Object

I have never encountered an issue like this before (if I had a dollar for every time I said that), so I'll do my best to explain it fully.

For this application I'm building, I'm using CoffeeScript for the client side code, and C# for the server side code.

On the client side I have an array named products. This holds a product name, as well as an id for that product.

I'm simply trying to send this array to the server. Here is what the client is doing:

 $http.put("#{$config.serverAddress}UserChoice/GetTotalScoreForProducts?productIDList=" 
+ _.pluck(products, 'id')
).success (response) ->
    console.log "SUCCESS"
    console.log response
  .error ->
    console.log 'error'

Here is the method on the server. Right now I'm just debugging this issue, so there is just a variable there for displaying the length of the array that I'm passing:

    [HttpPut]
    public void GetTotalScoreForProducts([FromUri] object[] productIDList)
    {
        int length = productIDList.Length;
    }

When debugging, the object[] productIDList only contains one object. However, that one object consists of both of the id's from the array:

enter image description here

So for some reason, the two seperate array objects are being put into one object.

The issue seems to be with how I'm passing the products array to the server, but I can't seem to figure out what I'm doing wrong.

Any help would be greatly appreciated.

Thank you for your time.

EDIT: After trying T.Rahgooy's solution:

enter image description here

EDIT: After hard coding in values to the URL:

enter image description here

EDIT: It's working now! Here's the working code!

$http.put("#{$config.serverAddress}UserChoice/GetTotalScoreForProducts?productIDList=" 
+ _.pluck(products, 'id').join(&productIDList=)
).success (response) ->
    console.log "SUCCESS"
    console.log response
  .error ->
    console.log 'error'

enter image description here

Thanks for helping me out T.Rahgooy =)

like image 204
Shaun O Donovan Avatar asked Jan 20 '26 02:01

Shaun O Donovan


1 Answers

You are sending querystring: ? productIDList=x,y, it should be querystring: ? productIDList=x &productIDList=y to receive as an array.

$http.put("#{$config.serverAddress}UserChoice/GetTotalScoreForProducts?" 
  + _.map(products, (x)->"productIDList=" + x.id + "&").join(' ')
).success (response) ->
console.log "SUCCESS"
console.log response
.error ->
console.log 'error'
like image 157
Taher Rahgooy Avatar answered Jan 21 '26 17:01

Taher Rahgooy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!