I am trying to parse a JSON object (not stored on a file) by using $.ajax() method but it is not functioning. What am I doing wrong?
var painting = [
{
"title": "Boardwalk 5",
"artist": "Arnie Palmer",
"image": "ap1.jpg",
"price": 850
},
{
"title": "A Lasting Piece",
"artist": "Arnie Palmer",
"image": "ap2.jpg",
"price": 450
},
{
"title": "Surf at High Tide",
"artist": "Arnie Palmer",
"image": "ap3.jpg",
"price": 950
},
{
"title": "The Games We Play",
"artist": "Arnie Palmer",
"image": "ap4.jpg",
"price": 850
}
];
$(document).ready(function () {
$.ajax({
type: 'GET',
url: 'painting',
dataType: 'json',
success: jsonParser
});
});
function jsonParser(json) {
$.getJSON(painting, function(data){
$.each(painting, function(k,v){
var title = v.title;
var price = v.price;
$('#container').append('<div class="painting"><br/><div class="title">' + title + '<br/>$' + price + '</div></div>')
});
});
}
$.getJSON? And you seem to be ignoring thejsonreturned form the$.ajaxcall. Why is that?$.getJSON(paintingto do?$.getJSONis a AJAX method.paintingis already an object, it's NOT a JSON string! Just get rid of$.getJSON(painting,.paintingvariable? No, it's not referencing JSON data. If it were JSON data, it would be simple text that represents a data structure. Sort of like XML is textual data representing a data structure. Within your program, thepaintingvariable will be referencing a JavaScript Array of Objects.