0

i need to pull a small string from a diffrent site from a div with the class 'entry' (theres only one div with that class and the div doesnt have an id). I learned about this plugin http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/ But i somehow did not manage to install nor use the code to make it work. I tried nothing but the code on the plugin page. Where/How do i need to install the plugin? Where/How to implement the given code correctly? Maybe a working fiddle sample would help :)

EDIT: I used this code

<script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script>
<script src="https://raw.githubusercontent.com/padolsey-archive/jquery.fn/master/cross-domain-ajax/jquery.xdomainajax.js"></script>

 <script type="text/javascript">
              jQuery(document).ready(function() {

                  jQuery.ajax({
                      url: 'http://news.bbc.co.uk',
                      type: 'GET',
                      success: function(res) {
                      var headline = jQuery(res.responseText).find('a.tsh').text();
                      alert(headline);
                  }
              });


          });
</script>

And i use phase5 HTML editor, so theres no error thrown, any recommandation for a different editor? The code just doesnt produce any result, the page loads and functions as normal but no alert is shown.

ueen
  • 692
  • 3
  • 9
  • 21
  • Hi, please post a section of your code so I can see that you have used the ajax correctly. Have you referenced the jQuery library in your project? What error do you get when you debug the Ajax method? – Dan Tromp Sep 27 '16 at 07:59
  • "*must include .. code necessary .. in the **question itself***" – freedomn-m Sep 27 '16 at 08:17
  • that post perhaps considers `Access-Control-Allow-Origin` of the responding server to be set appropriately. – techie_28 Sep 28 '16 at 10:21
  • I added the header header('Access-Control-Allow-Origin: *'); in the functions.php of the wordpresssite i want to pull the text from. Now i get a response, but its empty, any suggestions? – ueen Sep 28 '16 at 14:48

1 Answers1

0

OK i found a solution, here is what i did: Added header('Access-Control-Allow-Origin: *'); in die functions.php of the wordpress site i want to acess then i used the following ajax request to pull the content:

             jQuery.ajax({
                  url: 'http://www.somesite.wordpress.com/',
                  type: 'GET',
                  success: function(res) {
                      var data = jQuery.parseHTML(res);  
                             jQuery(data).find('div.class').each(function(){
                                 jQuery('#destination').append(jQuery(this).text());
                            });
                  }
              });

I tried all of this: Getting specific element from external site using jQuery / ajax and the last answer worked for me (its the same code mentoined above). Unfortunately i dont know why or how this works and if this is the best way, probably not - still it works somehow so thats fine by me. If anyone sees through this and knows a better/sleeker solution, it would be very welcome!

Community
  • 1
  • 1
ueen
  • 692
  • 3
  • 9
  • 21