How ajax does to identify classes

Asked

Viewed 147 times

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 properties . title for example.

  • Want to know how to know in javascript if the property exists or how ajax works internally and how it reads/treats the data received from the server? If this is the case for the first option, you can ask the question the return you give on the browser console console.log(produtos); within the ajax Success function?

  • I want to know how it works internally, how it talks to the server and reads/treats the data.

  • 3

    I’ll just comment on not being absolutely sure. In my opinion "he", AJAX, does not know. AJAX is the technique itself and despite having several features, it is not his responsibility to know a structure built by the application server-side. JSON, roughly speaking, is an associative array. And from what I can tell, JS is very flexible when it comes to this kind of "structure failure". In practical and direct terms, if the data exists, it works. If it does not work only to say that it does not exist (Undefined).

1 answer

5


If you post to the URL teste.aspx/InsertData containing the body {categoria:categoria } and request headers (for example, with the client Postman), you will receive a reply in format JSON. Who does this conversion of 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 reply from that request containing the whole body in JSON format and will make it available in the parameter of the success function (in this case produtos, but it could be any name).

Ajax (background request) assignments end there.

Henceforth it will be your responsibility to callback Javascript do something with the received response from the server. In case, as well placed by Bruno Augusto nothing is done to treat the answer other than the parse of JSON content in Javascript objects (the jQuery client knows that the type is JSON because you reported it in the parameter dataType, if the type had not been informed jQuery would try to infer the type through the MIME Type response).

At this point you can already view the entire answer object tree (e.g., with a Debugger).

Its function of callback has implicit knowledge about the schema JSON (i.e., you, the developer, know that objeto.d[] <-> List (Of Produto)) according to what was serialized in server side) and, knowing this, only iterates the objects within this array d displaying the titles.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.