Sto provando a passare un array di parametri stringa a un servizio Web ASP.NET C# utilizzando jQuery Ajax. Ecco il mio metodo web di esempio. Si noti che la funzione accetta un numero variabile di parametri. Quando eseguo jquery, ricevo un errore interno di 500 server nella console javascript di Chrome. Sto utilizzando jQuery 1.6.2 e .NET3.5Passare array di stringhe a webmethod con numero variabile di argomenti utilizzando jQuery AJAX
[WebMethod]
public string Concat(params string[] arr)
{
string result = "";
for (int i = 0; i < arr.Length; i++)
{
result += arr[i];
}
return result;
}
Ecco l'jquery:
$(document).ready(function() {
var myCars=new Array();
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";
$.ajax({
type: "POST",
url: "WebService.asmx/Concat",
data: {arr: myCars}, //can't figure out what to put here
success: onSuccess,
Error: onError
});
});
function onSuccess()
{
alert("testing");
}
function onError()
{
alert("fail");
}
</script>
Ogni aiuto è apprezzato!
forse si potrebbe [serialize] (http://api.jquery.com/serialize/) l'array e poi si unserialize nel servizio. – Shomz