0

What i'm trying to do is what i thought would be quite easy, but it doesnt seem to be working. I want to get the href of an object and all the function is returning is undefined.

This is the page that i'm requesting and what i'm trying to retrieve is the href held in the element of the first seller (who's ClassName is ui-link-inherit)

var buy = $.get(
    "http://m.roblox.com/items/24826737/privatesales/",
    function (data){
        alert($(data).find(".ui-link-inherit:eq(0)").attr('href'));
    }
);

I thought it was a permissions issue at first but it still wont work even if you run that on the page.

Alex
  • 1,035
  • 3
  • 16
  • 31
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Benjamin Gruenbaum Apr 13 '14 at 21:11
  • I tried a couple of the solutions suggested in that question and nothing really seemed to work. This isnt really complex so i'm not sure what's wrong with it. – Alex Apr 13 '14 at 21:17
  • Don't use the solutions, read the answers, carefully. When you do `var buy =` buy is not equal to the result of the AJAX response - since the AJAX result is not even ready yet, since it's asynchronous. – Benjamin Gruenbaum Apr 13 '14 at 21:18
  • Well then this should work too, but it doesnt: function getHREF(){ $.get( "http://m.roblox.com/items/24826737/privatesales/", function (data){ alert($(data).find(".ui-link-inherit:eq(0)").attr('href')); } ); } getHREF(); – Alex Apr 13 '14 at 21:21
  • I don't see a `.ui-link-inherit` element on the page. Do I need to be logged in? That could be the problem. – Thomas Apr 13 '14 at 21:31
  • @Thomas, I logged out and tried it and `ui-link-inherit` is still there. – Alex Apr 13 '14 at 21:39
  • I don't know why but I don't see it. What browser are you using? Do you have any cookies or other local data (localStorage, sessionStorage etc.) – Thomas Apr 13 '14 at 21:47
  • I'm using Chrome, but @Fuzzyma below (I think) is on to something. If the elements are loaded via javascript after the page has loaded then they wont be able to be fetched through GET – Alex Apr 13 '14 at 21:49

3 Answers3

1

Did you tried to just alert the data you get? If there is no .ui-link-inherit ofc it wont work and since .ui-link-inherit seems to be a class of jqueryUI which adds the classes after the page is loaded via javascript, you wont get this class via GET

//EDIT: I dont get all this "you cant access the data cause ajax is asynchronus". He is using the get-fukction completely right. He can access data since data IS the returned stuff from the server. Did I miss something that you all get this that way?

Fuzzyma
  • 7,619
  • 6
  • 28
  • 60
  • If you go to the page, this will work: `alert($(document).find(".ui-link-inherit:eq(0)").attr('href'));` – Alex Apr 13 '14 at 21:44
  • Are you using the console to execute this Javascipt? If so ofc the class is there cause it was added after DOM-load. How you execute the javascript on that site? – Fuzzyma Apr 13 '14 at 21:49
  • I think you're right about the javascript loading. I wont be able to load the elements if theyre loaded via JS – Alex Apr 13 '14 at 21:54
  • You are loading this side via ajax. That means you get the orginal html without any changes. To get the element you want you have to search for another path to the element. – Fuzzyma Apr 13 '14 at 21:56
  • I really dont know how to do that, but thanks anyways – Alex Apr 13 '14 at 22:01
0

You cannot return a value from that function, it is executed asynchronously. Instead just wait for the AJAX to finish and then do something with the result

// don't do this 
// var buy = $.get(... etc...);
// the variable buy will never have any value

// do this instead
function getHREF(){ 
    $.get(
          "http://m.roblox.com/items/24826737/privatesales/",
          function (data){
              var buy = $(data).find(".ui-link-inherit:eq(0)").attr('href');
              doSomething(buy);
          }
    );
)};

function doSomething(buy) {
    // in here do whatever you want with the ajax data 
}

$(document).ready(function () {
    getHREF();
});
JK.
  • 21,477
  • 35
  • 135
  • 214
-1

The site does not allow Cross-site HTTP requests. Read here: HTTP access control

jackfrankland
  • 2,052
  • 1
  • 13
  • 11
  • Just because your code as it is does not work on the site, does not mean that you will be able to get it to work on yours. – jackfrankland Apr 13 '14 at 21:36
  • I'm not worried about it working on my site. That wasn't my original question. – Alex Apr 13 '14 at 21:37
  • OK, sorry. If you wish to make an ajax call in a browser to this particular site, it will have to be run on the same domain. If you are not able to publish your site onto this domain, then it will not work. – jackfrankland Apr 13 '14 at 22:14