-1

I want to access a website and get the anchor tags from it using an "ajax call" as follows:

$.ajax({
    url: 'https://www.facebook.com/',
    type: 'GET',
    success: function(res) {
        var headline = $(res.responseText).find('a').text();
        alert(headline);
    }
});

But i get headline as blank. However i try the same code with a "http" website, it works fine.

Nandan Chaturvedi
  • 1,028
  • 3
  • 16
  • 32
  • 1
    It's not possible without Facebook actively permitting you; not with Ajax alone. Ref: [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy) and the [limited options for exceptions](http://en.wikipedia.org/wiki/Same_origin_policy#Relaxing_the_same-origin_policy). They do [have an API](https://developers.facebook.com/docs/reference/javascript/), though. – Jonathan Lonowski Feb 04 '13 at 09:28
  • First of all, you are not logged in this way (if that's your goal). Second to that a lot of code on Facebook is generated by JavaScript after the page is loaded. This will likely cause problems with the return data the XHR request gives back. What you can try first is to dump the returned data and confirm certain other things, like; is other JavaScript generated information loaded in my response?, (if you want to be logged in;) am I logged in? (you probably need to use a Facebook API to achieve this) –  Feb 04 '13 at 09:28
  • @Allendar Nandan said "get the anchor tags", so it seems to be an attempt to parse the returned HTML. [I guess the title is wrong, instead of "with username and password" it should be "with https"] – Theraot Feb 04 '13 at 09:30
  • @Nandan check this out: http://stackoverflow.com/questions/1012777/https-request-via-ajax-from-http-page – Theraot Feb 04 '13 at 09:35

1 Answers1

0

It's because https is something different than http, and Facebook just like many other websites uses cookies for authenticating. You have to first authenticate in https://facebook.com and then you can get data back.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188