Suppose I have a webpage on the web, this page has many elements with ids. How do I fetch these elements using javascript?
I’ve used this chunk with jQuery:
$.get(websiteUrl, {}, function(results){
//alert(results); // will show the HTML from anotherPage.html
console.log($(results).find("bookId").html()); // show "bookId" div in results
});
But I get error:
XMLHttpRequest cannot load; Origin http://127.0.0.1:8000 is not allowed by Access-Control-Allow-Origin.
Actually, I’m trying to fetch some data from webpage in Phonegap app on Android.
Any ideas how to do this?
Most browsers adhere to a strict same-origin policy. Meaning you can’t make Ajax requests to a different server. While you can bypass it, it’s not practical to do with every client.
Your best bet would be to invoke a server-side script to fetch it for you, and return the results to JavaScript.