2015-03-06 4 views
5

Sto provando a passare l'array di stringhe a un metodo Web Api che accetta un array di stringhe come argomento. Bellow il mio Web metodo APIPassare array di stringhe come dati in jquery ajax su web API

[HttpGet] 
    public string HireRocco(string[] hitList) 
    { 
     string updateList = string.Empty; 
     return updateList; 
    } 

mio ajax

var uri = 'http://localhost:16629/api/AssassinApi/HireRocco', 
hitList = ['me', 'yourself']; 

$.ajax({ 
    url: uri, 
    type: 'GET', 
    data: { hitList : hitList }, 
    cache: false, 
    dataType: 'json', 
    async: true, 
    contentType: false, 
    processData: false, 
    success: function (data) { 
    }, 
    error: function (data) { 
    } 
}); 

È possibile che questo Ajax colpisce con successo il metodo HireRocco ma hitList param è ancora nulla. Cosa dovrei cambiare per passare un array di stringhe come param.

+0

'tipo: 'POST''? –

+0

@Rakesh_Kumar: è un metodo GET, comunque se faccio un POST non risolverà il problema – Riki

risposta

0

Rimuovere contentType: false quindi impostare processData su true in modo che possa aggiungere postData all'url, in quanto è così che funziona una richiesta get o sarà necessario modificare l'API per accettare la richiesta POST impostata tramite l'intestazione.

$.ajax({ 
    url: uri, 
    type: 'GET', 
    data: { hitList : hitList }, 
    cache: false, 
    dataType: 'json', 
    async: true, 
    processData: true, 
    success: function (data) { 
     console.log(data); 
    }, 
    error: function (data) { 
    } 
}); 
3

Se è necessario inviare i dati tramite un HttpGet, è possibile aggiungere [FromUri] è possibile modificare la vostra azione di controllo come segue e dovrebbe funzionare il tuo JavaScript come è:

[HttpGet] 
public string HireRocco([FromUri] string[] hitList) 
{ 
    string updateList = string.Empty; 
    return updateList; 
} 
0

Prima di tutto vi suggerisco usi POST piuttosto che GET. crea un array javascript. spingere i dati al suo interno. inviarlo al metodo di azione web api utilizzando JSON.Stringify .. e quindi elaborare l'ulteriore logica.

Nel web api creare una variabile del modello .. e creare un oggetto elenco ..

seguito riporta il codice ..

Javascript

var demoarray=[]; 
demoarray.push({"test1":"hi", "test2":"hello"}); //test1 and test2 are model variable names in web api and hi and hello are their values 

è possibile ripetere il processo in ciclo for o qualcosa per aggiungere più valori.

$.ajax({ 
     url:"http://localhost..", 
     type: "POST", 
     data: JSON.Stringify(demoarray), 
     contentType: "application/json", 
     success: function(data) 
       { 
       }, 
     error: function(data) 
      { 
      } 
     }); 

WEB API Codice Crea una classe del modello e due proprietà

public string test1 {get; set;} 
public string test2 {get; set;} 

codice del controller

[Httppost] 
public void actionmethod(List<modelclass> obj) 
{ 
    int i=0; 
    for(i=0; i<obj.count; i++) 
    { 
    //your logic 
    } 
} 
Problemi correlati