将JSON转换为WebMethod中的对象列表

时间:2018-12-04 15:58:48

标签: jquery asp.net ajax

我有以下变量:

var atributos = [{Atributo: "Autor", Valor: ""},
{Atributo: "Titulo", Valor: ""},
{Atributo: "Documento de/até", Valor: " :: "},
{Atributo: "Conteudo", Valor: ""}]

这个AJAX请求:

 $.ajax({
        type: "POST",
        url: "GestaoFicheiros.aspx/FuncaoPesquisar",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: {Atributos:JSON.stringify(atributos)},
        async: false,
        success: function (data) {

        },
        error: function (xhr, textStatus, errorThrown) {
            console.log(xhr);
            alert('request failed:\nxhr:' + xhr + '\nTextSatus:' + textStatus + '\nError thrown:' + errorThrown);
        }
    });

但是,它给了我一个500错误。 WebMethod如下:

[WebMethod]
public static void FuncaoPesquisar(List<AtributoInput> Atributos){

    Debug.WriteLine("received");

    foreach(AtributoInput attr in Atributos)
    {
        Debug.WriteLine(attr.Atributo + " - " + attr.Valor);
    }

}

还有Atributoinput类:

public class AtributoInput
{
    private string _Atributo;
    private string _Valor;
    public string Atributo
    {
        get { return _Atributo; }
        set { _Atributo = value; }
    }
    public string Valor
    {
        get { return _Valor; }
        set { _Valor= value; }
    }
}

有人知道为什么这行不通吗? WriteLine无法打印。

谢谢!

0 个答案:

没有答案