0

https://i.stack.imgur.com/9Pha0.png This is the part of my JSON. I need to access photo_file_url

I tried to write it as

$.ajax( {
        url: "http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=1&minx="+longitude+"&miny="+latitude+"&maxx="+lonmax+"&maxy="+latmax+"&size=original&mapfilter=true",
        type: "get",
        success: function(response) {
        document.getElementById("back").innerHTML = response.photos.photo_file_url;
        }
});

but it doesn't work

irynabond
  • 1,059
  • 2
  • 11
  • 18
  • is `photos` an array/list of photo objects? If so you need to access those via their index first. – lemieuxster Jul 30 '15 at 22:49
  • 1
    possible duplicate of [how to parse json data with jquery / javascript?](http://stackoverflow.com/questions/8951810/how-to-parse-json-data-with-jquery-javascript) – klenium Jul 30 '15 at 22:54

5 Answers5

0

Since you are doing an async GET request, you can also use

$.getJSON("http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=1&minx="+longitude+"&miny="+latitude+"&maxx="+lonmax+"&maxy="+latmax+"&size=original&mapfilter=true", function(response) {
  // response will be a json object
  document.getElementById("back").innerHTML = response.photos.photo_file_url;
});

I recommend this solution only because you are doing an asynchronous GET request which is synonymous with $.getJSON()

ddavison
  • 28,221
  • 15
  • 85
  • 110
0
$.ajax( {
        url: "http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=1&minx="+longitude+"&miny="+latitude+"&maxx="+lonmax+"&maxy="+latmax+"&size=original&mapfilter=true",
        type: "get",
        success: function(response) {
        response = $.parseJson(response); //parse json object
        document.getElementById("back").innerHTML = response.photos.photo_file_url;
        }
});
super-user
  • 1,017
  • 4
  • 17
  • 41
0
success: function(response) {
    document.getElementById("back").innerHTML = JSON.parse(response).photos[0].photo_file_url;
}

Or

$.ajax({
    dataType: "json"
    succes: ...

Or $.getJSON

klenium
  • 2,468
  • 2
  • 24
  • 47
0

Ajax-Parse JS/JQuery

How to access JSON object in JavaScript

I don't know how helpful these will be but hopefully it's a starting point.

Community
  • 1
  • 1
python490
  • 11
  • 1
-1

Your response is a string, you can convert it to an object using JSON.parse.

EDIT: For compatibaility $.parseJSON. See this answer.

Community
  • 1
  • 1
Ivan
  • 10,052
  • 12
  • 47
  • 78