1
I have a link with an attribute that has the information of which JSON data should be obtained.
<a href="#" data-json-code="code_118"></a>
And I have the code responsible for its behavior, which should be:
- Take the attribute;
- Find this attribute in json;
- View json information in the console.
I did this way, but always returns "Undefined":
$('.c-productCard a').on('click', function(event) {
event.preventDefault();
var jsonCodeProduct = $(this).attr('data-json-code');
var jsonUrl = 'https://api.myjson.com/bins/mc9ig';
$.ajax({
type: 'GET',
datatype: 'json',
url: jsonUrl,
cache: false,
beforeSend: function() {
console.log('before send');
},
success: function(result) {
console.log(result.jsonCodeProduct);
},
error: function(xmlHttpRequest, textStatus, errorThrown) {
console.log('erro');
},
complete: function() {
console.log('complete');
}
});
});
If I happen to remove the jsonCodeProduct
of the call success
the entire json is displayed in the console (as expected).
The question is, how to make the variable die jsonCodeProduct
be the "selector" to display the corresponding data from JSON
?
Have you solved? present the structure of your Json, so I can better elaborate my answer.
– Leandro Angelo
I got @Leandroangelo, thank you very much, the solution was just what you indicated, just put the "jsonCodeProduct" in brackets after the result.
– Guilherme Laureano
So please mark the proposal as the answer to your question, it will help other people who are going through the same problem to find the fastest solution.
– Leandro Angelo