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?– Sergio
I want to know how it works internally, how it talks to the server and reads/treats the data.
– Diego Zanardo
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).
– Bruno Augusto