Receive and display an array of C # in JavaScript

1

I have an array of C # that I need to show when I enter a certain page. I am making a request with AJAX to receive this data.

$.post({
        url: '/Home/MostrarArray',
        success: function (datas) {
            console.log(datas);
        }
    });

These being my AJAX request and:

[HttpPost]
public string[] MostrarArray()
{
    string[] matArray = new string[] { "uno", "dos", "tres", "cuatro" };

    Array.Sort(matArray);
    return matArray;
}

The function that I call.

What it returns to me (what console.log(datas) shows) is a String , literally: System.String[]

I'm not sure how to get the array back and not the "type" of data ... Any idea what I'm doing wrong?

    
asked by Rabegi 09.05.2018 в 11:12
source

1 answer

1

JavaScript (JQuery in this case) does not understand the native types of C # (in this case, string[] ).

To perform the return of parameters in these cases, it is best to serialize the value to return as json (using for example Json.NET ), and receive them in the same way using datatype: 'json'

    
answered by 09.05.2018 / 13:11
source