POST array of objects to REST API

后端 未结 2 508
情深已故
情深已故 2021-01-08 00:10

I\'m designing REST API that should be able to accept array of objects, say

[
 {
   \'name\': \'Alice\',
   \'age\': 15
 },
 {
   \'name\': \'Bob\',
   \'age         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-08 00:40

    There is no need to wrap the array in another object with a data property. The array by itself is valid JSON:

    post_params = JSON.stringify([ { 'name' : 'Alice', 'age' : 15 },
                                   { 'name' : 'Bob',   'age' : 20 },
                                      ...
                                 ]);
    post(url, post_params);
    

    Just make sure your API expects this array as well.

提交回复
热议问题