0

Having worked through previous postings and results of other searches, I seem to be able to get what I want but it appears to be a data object rather than text that I can assign to a variable.

I am limited to javascript in the environment I am working in and what I want to do is to use string functions on the returned data so that I can search for certain terms.

This is the url format that returns the data object - the result is that you get asked to open or save a text file:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%20%3D%20%22http://www.redrag.info/index.htm%22%20and%20xpath%3D%22*%22&format=xml&callback=cbfunc

(you can replace my url - http://www.redrag.info/index.htm - within the example above with one of your own if you wish)

Instead of opening the text file, can I use some javascript document functions to assign this output to a variable?

Thanks.

RedRag
  • 13
  • 3
  • Welcome to StackOverflow (SO)! You say that getting the page source *almost* works, yet there is no evidence of that in the question. Please include the code that you tried to use and what errors you encountered. – Artjom B. Nov 19 '14 at 12:42
  • Hi @Artjom B. - by "almost works" I meant that it nearly achieves my objective - if you open the url it offers to either save a text file or open notepad and display it when what I wanted to do was to get this text assigned to a variable – RedRag Nov 20 '14 at 17:22

3 Answers3

0

Remove callback=cbfunc and define yours callback function. and you can change format=json from format=xml because it is simple to handle. Now you get data and do whatever you want with that data

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%20%3D%20%22http://www.redrag.info/index.htm%22%20and%20xpath%3D%22*%22&format=json",
function(data) {
    alert(data);
}
);

http://jsfiddle.net/kriyeta/Lo3m4gb2/4/

kriyeta
  • 695
  • 4
  • 12
  • Works perfectly - thanks for your contribution - very much appreciated – RedRag Nov 19 '14 at 17:04
  • Hi again @kriyeta - would there be a way of achieving same result without using jquery library since one of my target urls fails when it detects jquery.js for some reason – RedRag Nov 20 '14 at 17:33
  • @RedRag It is already well explained in [this post](http://stackoverflow.com/questions/6132796/how-to-make-a-jsonp-request-from-javascript-without-jquery) – kriyeta Nov 20 '14 at 18:43
  • thanks @kriyeta, I checked that post you suggested and made a couple of jsfiddles - first the one that works using jquery library - http://jsfiddle.net/redrag/kxswvx8d/ and then another to test with no jquery using the code from the post you suggested with 'no library' set - http://jsfiddle.net/redrag/kxswvx8d/16/ which doesn't produce a result so maybe I am misunderstanding how to apply the code from that post? – RedRag Nov 21 '14 at 11:47
0

That requires JSONP (Parsing the response an an inline script), trivial with a library like jQuery:

$.ajax({
  url: the_url,
  jsonpCallback: 'cbfunc',
  dataType: 'jsonp'
})
.success(function(data) {
    alert(data.query.created);
});

http://jsfiddle.net/alexk/92erv4v5/

Community
  • 1
  • 1
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Hi @Alex, thank you for this although I couldn't get it working in the jsfiddle. – RedRag Nov 20 '14 at 17:34
  • I see - I changed alert(data.query.created) to alert(JSON.stringify(data) in your jsfiddle and see that it does work. Thanks – RedRag Nov 21 '14 at 12:07
0

Further to the first answer above from @kriyeta, I have the code working nicely as shown in this code segment:

    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>
    <body>
    <script>
    var inputxt="http://www.redrag.info/index.htm";
    var pagesrc;

    $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%20%3D%20%22"+inputxt+"%22%20and%20xpath%3D%22*%22&format=json",
    function(data) {
    pagesrc=JSON.stringify(data);
    alert(pagesrc);
    }
    );
    </script>
    </body>
    </html>

But when I use as a target URL: https://ajax.googleapis.com/ajax/services/search/images?v=1.0%26q=DOG (which if you put into a browser address bar to see the actual page you need to change the %26 back to &) I get a 'Suspected Terms of Service Abuse' message because I think Google can determine that it is an automated request rather than a human request.

What I am trying to achieve is a script that I can use with an AI virtual assistant facility where I am currently developing the knowledge base. If the google url didn't fail then the code above would be just what I need.

It is to answer a visitors request such as 'show me a picture of a dog'. Another AI bot - 'Jeanie' at https://ask.pannous.com/ does exactly this - it retrieves a picture from (I'm assuming) Google Images when asked the same question - so I know it can be done and I'm keen to have a similar facility.

I think if I can find a solution that doesn't rely on the jQuery library then it might work.

Grateful for any ideas. Thanks.

RedRag
  • 13
  • 3