2017-10-17 3 views
1

L'API (écrit en C#, ASP.NET de base) reçoivent 3 champs: titre, le contenu et un tableau de catégories (entiers):Envoyer tableau à l'API

[HttpGet] 
[AllowAnonymous] 
public IActionResult CreatePublication(string title, string content, IEnumerable<int> categoryIds) 
{ 
    return Ok(); 
} 

Le problème est l'API reçoit toujours zéro éléments dans le tableau categoryIds.

Le client Angular4:

let ccategoryIds = new Array<number>() 
ccategoryIds.push(2) 
ccategoryIds.push(5) 
ccategoryIds.push(7) 

let requestOptions = { 
    params: new HttpParams() 
    .set('title', 'The title') 
    .append('content', 'The content') 
    .append('categoryIds', ccategoryIds.toString()), 
    withCredentials: true 
} 

this.http 
    .get('http://localhost:53203/api/publications/createPublication', requestOptions) 
    .subscribe(data => { 
    }, 
    (err: HttpErrorResponse) => { 
    alert('Error') 
    }) 

Mise à jour

Si je JSON.stringify (ccategoryIds) au lieu de ccategoryIds.toString(), je reçois zéro éléments dans la API aussi.

enter image description here

Il est pas un problème du back-end. En Postman cela fonctionne: enter image description here enter image description here

Répondre

1

Évidemment, je ne vois pas comment vous avez écrit le gestionnaire de l'API, mais je mince que vous utilisez .toString() où vous devez utiliser JSON.stringify():

let ccategoryIds = new Array<number>() 
ccategoryIds.push(2) 
ccategoryIds.push(5) 
ccategoryIds.push(7) 

let requestOptions = { 
    params: new HttpParams() 
    .set('title', 'The title') 
    .append('content', 'The content') 
    .append('categoryIds', JSON.stringify(ccategoryId)), 
    withCredentials: true 
} 

this.http 
    .get('http://localhost:53203/api/publications/createPublication', requestOptions) 
    .subscribe(data => { 
    }, 
    (err: HttpErrorResponse) => { 
    alert('Error') 
    }) 
+0

@JohnDoe cette semble être un problème du côté C#. Pouvons-nous voir votre code là-bas? –