3

I'm wrinting on a PhoneGap app that has the HTML and Javascript for the Navigation local and it should load the contend from the Web.

I don't have direct controll over the content Server so I can't change anything there.

The Content i want to get in to my app is based on a normal HTML website so i want to get for example the Text in da div or so.

What i have so far:

<script type="text/javascript">
  var url = "http://example.org/"
  updateGadget();

  inervalID = setInterval("updateGadget();", 60 * 1000);

  function updateGadget() {
    $(document).ready(function () {
        $.ajax(url, {}, function (response, status, xml) {
            console.log(response);
        });
    });
  }
</script>

The Problem is thad it doesn't work, it shows me nothing.

For developing is use a Webserver not directly PhoneGap. When i open the Website in Google Chrome it shows the error:

XMLHttpRequest cannot load http://example.org Origin http://example.org is not allowed by Access-Control-Allow-Origin.

in the Console.

I found the i should use this header:

header('Access-Control-Allow-Origin: *');

Where should i use it, if i put it in my HTML (PHP) file to the Top it does nothing an on the Server i want to Parse I cant put it.

Where is the Problem, and how can if fix thad? Or is there a better Way to do this? If possible i want to di it directly on the PhoneGap app without a secound server backend.

daniel__
  • 11,633
  • 15
  • 64
  • 91
user2436057
  • 195
  • 11
  • 1
    You must [whitelist all domains you use](http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html) in your PhoneGap application. – apsillers Jun 14 '13 at 12:54
  • @apsillers at the Moment i run the website und a webserver for testing and not on PhoneGap. Is there a Way to do it then? – user2436057 Jun 14 '13 at 13:52
  • 1
    The site that is *offering the page* must serve `Access-Control-Allow-Origin: *`. This is a way for the server to say "yes, I will allow browsers to use this page cross-origin." If you don't control the website, you don't have the ability to say whether browsers can use it cross-domain or not. Perhaps you could try using a proxy like http://whateverorigin.org/, or [disable your browser's adherence to the same-origin policy](http://stackoverflow.com/q/3102819/710446) (but note that this should be done during testing *only* -- it's unsafe to leave this disabled during normal browsing). – apsillers Jun 14 '13 at 14:05
  • I agree with apsillers. Moreover, using a server-side scripting (e.g. php) allows you to cache (e.g. APC) your parsed contents and improve response time. – Martin Jun 16 '13 at 19:33
  • See what error code you get... use $.ajax({ url: 'http://example.org/', data: myData, type: 'GET', dataType: 'jsonp', //whatever is your data type success: function() { alert("Success"); }, error: function(jqXHR, textStatus, errorThrown) { alert('Failed!' + textStatus); } }); – VJS Oct 04 '13 at 06:58

1 Answers1

1

This stackoverflow question has a solution for you.

Basic idea is to make $.get request at http://whateverorigin.org/get?url=webpage-you-wish-to-scrap.com

Community
  • 1
  • 1
Asur
  • 1,949
  • 4
  • 23
  • 41