How ajax does to identify classes

5

I have the following application:

        function atualizarPrdutos(categoria){
            $.ajax({
                type: "POST",
                url: "teste.aspx/InsertData",
                data: JSON.stringify({categoria:categoria }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (produtos) {
                var prods = produtos.d;
                    $.each(prods, function (index, prod) {
                        alert(prods.titulo);
                    });
                }
            });
        }

And in the test.aspx.cs file:

Public Class Produto
    Public id As Integer = 0
    Public titulo As String = ""
    Public imagem As String = ""
    Public valor As Decimal = 0
End Class

    <WebMethod()> _
    Public Shared Function InsertData(categoria As String) As List(Of Produto)        
        MyConnection.Open()
        Dim comando2 As New OleDbCommand
        comando2.Connection = MyConnection
        comando2.CommandText = "SELECT id, nome, valor, FotoVitrine from loja_produtos WHERE Menu=" + categoria
        Dim reader2 As OleDbDataReader = comando2.ExecuteReader()

        Dim list As New List(Of Produto)()
        While reader2.Read
            Dim produto As New Produto()
            produto.id = reader2.Item("id")
            produto.titulo = reader2.Item("nome")
            produto.valor = reader2.Item("valor")
            produto.imagem = reader2.Item("FotoVitrine")
            list.Add(produto)
        End While
        Return list
    End Function

My question is: How can ajax identify that the list has the .title properties for example.

    
asked by anonymous 30.05.2014 / 14:04

1 answer

5

If you make a post to the URL teste.aspx/InsertData containing the body {categoria:categoria } and request headers (for example, Postman ), you will receive a reply in the form JSON . Who does this conversion from your VB.NET object to JSON is your own web service (server side). In this case of VB.NET, it will automatically serialize the list of products to JSON format.

The client side in turn will receive the response from this request containing the entire body in JSON format and will make it available in the success function parameter (in this case produtos , but could be any name) .

The Ajax (request in background) assignments end there.

From there on, it will be the responsibility of your callback JavaScript function to do something with the response received from the server. In case, as well placed by Bruno Augusto nothing is done to handle the response in addition to the parse of JSON content in JavaScript objects (the jQuery client knows that the type is JSON because you reported it in the dataType parameter if the type had not been entered jQuery would try to infer the type through the MIME Type of the response).

You can now view the entire object tree of the response (e.g., with a debugger ).

Your callback function has implicit knowledge about the schema JSON (ie you, developer, know objeto.d[] <-> List (Of Produto) ) as serialized in < on> server side ) and, knowing that, it only iterates the objects within that array d displaying the titles.

    
30.05.2014 / 15:21