0

I wanted to get like count numbers from this link

http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.apple.com

var xml = "http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.apple.com;"
var xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc),
$title = $xml.find( "like_count" );
$('.countnumber').text($title.text());

however, I got this error

Uncaught Error: Invalid XML: http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.apple.com;

Is there a way to parse it by javascript?

Thanks

3
  • 1
    What does xml contain? Commented Oct 18, 2013 at 2:30
  • 1
    You have to get your xml first, you're only giving jquery a link to an xml document Commented Oct 18, 2013 at 2:31
  • because the contents of xml is not a xml value.. it is a url... you need to read the contents of the url first Commented Oct 18, 2013 at 2:32

3 Answers 3

2

In your case xml contains a resource url, not a xml string that is the cause of the error. You need to read the contents of the url first then parse it.

You need to read the contents of the URL

var xml = "http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.apple.com;"
$.get(xml).done(function(xml){
    var $xml = $(xml),
        $title = $xml.find( "like_count" );
    console.log($title.text())
})

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned in the comments retrieve your xml first.

$.ajax({
    url: "http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.apple.com;",
    dataType: "xml"
}).done(function(data) {
    var $xml = $(data),
        $title = $xml.find("like_count");
    $('.countnumber').text($title.text());
});

Comments

1

You need a string representing your xml, not the URI to the resource. Also, parseXML isn't even neccessary.

You need to use AJAX to get the XML:

    var xml = "http://api.facebook.com/restserver.php?       method=links.getStats&urls=http://www.apple.com;";

    $.get(xml, function(data){
      var xmlDoc = $(data);
      .. your code here
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.