0

I want to include only the google results (Div id="ires") for the given URL part. I encountered for example the "No 'Access-Control-Allow-Origin'"- Problem and I wonder how to solve this.

I started a JSfiddle here with my try: http://jsfiddle.net/23z7w3xp/1/

HTML:

<div id="results">Waiting for results</div>
<input type="button" id="btn" value="Show results">

JS:

$("#btn").click(function () {
    var requesthtml = "https://www.google.de/?gfe_rd=cr&ei=pXVyVKCDJKeK8QfkkoDYCg#q=test";

    $.get( requesthtml, function( data ) {
      var htmlcontent = data;
      $('#results').html(htmlcontent.find('#ires').text());
      alert( "Load was performed." );
    });
});
kentor
  • 13,371
  • 18
  • 72
  • 131
  • It is completely impossible to do that. – SLaks Nov 24 '14 at 00:25
  • It isn't for sure. Maybe only with javascript, but I can use php ofcourse as well – kentor Nov 24 '14 at 00:26
  • 1
    You can't access another domain's content directly via JavaScript unless they specifically allow CORS and/or provide a JSONP endpoint. One way to do it is via a backend "proxy" or something similar. Here is a simple example project where it's done with PHP and jQuery Ajax: https://github.com/vasilionjea/google-analytics-verify Please note that I can't advise you on the legality of it though. – istos Nov 24 '14 at 00:35

1 Answers1

1

What you want to do is illegal. You cannot take content of someones website and embed it in yours. But if you want to use the google search within you website, you should take a look at google's custom search

Here is a code example how it would work:

<html>
  <head>
    <title>JSON/Atom Custom Search API Example</title>
  </head>
  <body>
    <div id="content"></div>
    <script>
      function hndlr(response) {
      for (var i = 0; i < response.items.length; i++) {
        var item = response.items[i];
        // in production code, item.htmlTitle should have the HTML entities escaped.
        document.getElementById("content").innerHTML += "<br>" + item.htmlTitle;
      }
    }
    </script>
    <script src="https://www.googleapis.com/customsearch/v1?key=YOUR-KEY&amp;cx=017576662512468239146:omuauf_lfve&amp;q=cars&amp;callback=hndlr">
    </script>
  </body>
</html>

source

Daniel Budick
  • 1,680
  • 1
  • 18
  • 20