开发者

parsing a reponse from a XMLHttpRequest

开发者 https://www.devze.com 2023-02-17 16:54 出处:网络
I\'m struggling with how to parse a response from an XMLHttpRequest. The response is in json format: http://flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bb

I'm struggling with how to parse a response from an XMLHttpRequest. The response is in json format:

http://flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=paris&format=json

to make sure it does indeed come in as such i tested it:

document.getElementById('info').innerHTML = this.responseText

which returns me a page with a long line of data written in json format. Could someone help me figure out the next steps in order to extract data from the response i.e. a list of all titles

i did some research and came across this:

response = this.responseText ;
var doc = new DOMParser().parseFromString(response, "text/xml");

what do i need to do next? (Note: i wish to do this manually i.e. without the help of jQuery or similar tools.)

[EDIT]

based on the suggestions below and on the Flickr page on that matter, i have tried the following:

request.onreadystatechange = function()
{
 ...
            if (this.responseXML != null)
            {
                jsonFlickrApi(this.responseText) ;

                function jsonFlickrApi(rsp){

                    for (var i=0; i<开发者_JS百科rsp.photos.photo.length; i++){

                        var blog = rsp.photos.photo[i];

                        var div = document.createElement('div');
                        var txt = document.createTextNode(photo.owner);

                        div.appendChild(txt);
                        //document.body.appendChild(div);
                        document.getElementById('info').innerHTML.appendChild(div);
                    }
...
}

this doesn't return anything visible yet.

[EDIT2]

further troubleshooting reveals:

rsp = this.responseText ;
document.getElementById('info').innerHTML = rsp.stat ;

prints undefined


The URL you've given returns somethins like this :

jsonFlickrApi({"photos":{"page":1, "p ... , "stat":"ok"})

So, basically, it looks like Javascript code, which :

  • Calls the jsonFlickrApi function,
  • Passing it a big JSON object as a parameter.


First of all, here, you are working with JSON, so you should not use any DOM-related stuff : DOM functions' goal is to help manipulate XML.


Instead, you should :

  • Write a jsonFlickrApi function,
  • Make sure it's called when you receive the data from Flickr

About that, you shuld find a bit more informations, and an example, here : http://www.flickr.com/services/api/response.json.html


Else, adding the &nojsoncallback=1 parameter at the end of the URL of your request, you'll get pure-JSON as a result (and not a function-call).

That would allow you to use standard JSON-manipulation functions to work with that data, not having to implement any specific function.

Between those solutions, up to you to choose which one you prefer :-)


A different alternative is not to use JSON at all, and use XML instead. Leave out the format=json part of the URL and you get the data as XML. This XML can be parsed, for example with the DOMParser() method you tried, or with this.responseXML. However, the "logistics" of using XML, compared to JSON, are a bit more complicated, as you're browsing a DOM tree and not a JS object.

Update:

So here's one of the murky details of AJAX. Depending on the browser, you can't just make XML requests between domains. The following code will work (return something useful) on Safari, but not Firefox or Chrome. (There, it will return null or empty strings.) The JSON requests seem to work fine without on all browsers, however.

<script>
function createXHR(){
  if (window.XMLHttpRequest){
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject){
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}

function getFlickr(){
  xmlhttp=createXHR();

  url="http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=paris&";
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function stateChanged(){
  if (xmlhttp.readyState==4){
     alert(xmlhttp.getAllResponseHeaders());  
     alert(xmlhttp.responseXML)
     alert(xmlhttp.responseText)
     var xmlDoc=xmlhttp.responseXML.documentElement;

  }
}

getFlickr();
</script>


The cool thing about JSON is that it's actually executable code. You don't need to do any "manual" parsing – just run the code. Perhaps Flickr supplies a function called jsonFlickrApi with their API libs that they exepct you to use, but you could just as well supply your own.

function parseFlickrJson(jsonstring){
    var data=null;

    var jsonFlickrApi=function(d){
        data = d;
    }

    eval(jsonstring);

    return data;
}

myreturndata = parseFlickrJson(response);

// Try getting something from the object
alert(myreturndata.photos.pages);
0

精彩评论

暂无评论...
验证码 换一张
取 消