Is it possible to find the attributes of certain elements on one website and display them on another website? For example, if I have website 1, can I use Javascript/jQuery to find out the size of a specific image or div on website 2 and display those attributes on website 1?
If I can't do something like that with Javascript, is there an alternative way of going about accomplishing that specific example?
1. What you are trying to do can't be done using any AJAX library. Browsers' cross-domain policy won't allow you to do this.
But you can do this with a combination of php (or any other server-side language) and AJAX. Create a php script like this:
<?php
$url=$_POST['url'];
if($url!="")
echo file_get_contents($url);
?>
Let us say the script's name is fetch.php.
Now you can throw an AJAX call from your jQuery code to this fetch.php and it will fetch the HTML code for you.
2. The same origin applies. try this code and you'll face security error,
$.get("other web page site", {}, function(content){
$("#receipe").html(content)
}, "html")
3. Using Greasemonkey, it is possible to make third-party requests. A jQuery-oriented tutorial is offered on this page. The short answer it to have Greasemonkey make the request on your behalf. Replace all your XMLHttpRequest objects with GM_xmlhttpRequest objects.
Useful links,
Can Javascript read the source of any web page?
http://www.sitepoint.com/forums/showthread.php?836704-How-to-get-contents-of-3rd-party-website-into-javascript-variable
Sadly because of same origin policy you can't access the DOM on a different domain. If you control both domains you maybe able to use CORS and modify the server HTTP headers to allow Javascript access.
The workaround to this is to use a server to act as a proxy between the two websites. So you would have a server side script on website 1 that would send a request to website 2 and return the content from website 1.
Related
so I had an http call using angularjs
$http.get(link).then(function(data, response, header) {
console.log(data.data.url);
});
which is returned with an JSON body like this
{url:"http://www.example.com/article/1"}
and I want to display it to the container that I've made, let say the container is this
<div id="containerArticle"></div>
I tried it using iframe but it gives me an error Load denied by X-Frame-Options which is the domain is forbid me to using iframe.
my question is, what is the best approach of displaying content of another domain using angualrjs ? I've been stuck for 2 days now..
You can consider JSONP if you are only doing Get requests.
Another approach is to create a proxy page on your server that in turn makes a request to the other domain. This is often referred to as a reverse proxy.
I am doing a jquery.ajax() call on one of our pages to fetch a small text file. I see some of the requests (not all) fail with resp.statusText: "No Transport" and resp.status : 0
What does the error mean (No Transport with a resp code of 0). Strangely it works on some browsers, and doesn't work on some. I couldn't find a patter by looking at the user agents of browsers, where it failed.
Any help would be highly appreciated. I am a beginner to javascript and jquery library, let me know if I omitted crucial information.
My use case:
abc.mydomain.com contains jquery.ajax(url:xyz.mydomain.com) call
Most likely it prevents you from firing a request because it things you are trying to access another domain. xyz.mydomain.com !== mydomain.com.
Why that is not allowed?
Read
Use a Web Proxy for Cross-Domain XMLHttpRequest Calls
Why the cross-domain Ajax is a security concern?
An example to why this is a security issue, assume you installed a bad plugin to your browser. If that plugin got the permission, it can read all loaded files to your browser and be able to edit/change/inject content and codes. Then it might send all collected data to designer own server.
... The most common business needs that are easily accomplished with browser plug-ins are: modify default search, add side frames, inject new content into existing webpage ...more
A good practice is to fetch the data thru ajax via JSON, if you are trying to access another site beside the one the script is calling from, then use JSON-P.
Read
JSON-P
JSON-P call to subdomain
Chrome ajax call to subdomain
A common architecture is to call the current domain that the script is loaded from, then use server script to fetch data from the other domain where the other domain will response to the request and return the data.
A code snippets of your function will help us understand your issue more.
I am writing a project thesis and have stumbled on an issue which might set my entire project in doubt. So I just want to triple-confirm before I take any further actions.
I have a javascript file on URL1. The javascript code, for simplicity, retrieves information from URL2.
No matter how much I tried this wouldn't work so I did a Google search and figured out that the same-origin policy might be the reason.
So I ask you again, is this impossible?
this is totally possible, but there are some restrictions to it;
You can access information of URL2 from URL1, if URL2 provides a JSONP way. You'd generate a script tag that loads a js file (url2/information?id=123&callback=mycallback) in which the JSON is put into the specified function (in this case mycallback). The script on URL2 would look like this;
mycallback({"json":"information"});
Thus, URL1 needs a mycallback function defined and will get the information from URL2
A different approach would be to utilize XMLHttpRequest Level2 which can go cross-domain.
Another would be to communicate over onmessage/postmessage and have URL2 iframed within the page of URL1. This goes Cross-Origin as well.
There are other technics to make a hole in the same origin policy, like Hashs (but are very limited in comparison to the above technics)
I'm hosting a page on an sharepoint site, and need to pull content from multiple other pages. The content I need is on a different domain in a div, so I cannot use an iFrame. I've tried the following code with JQuery attempting to load the stcakoverflow container div from the landing page, but this doesn't seem to work. I'm assuming this is due to different origin policy:
<script>
$(document).ready(function() {
$('#LoadMe').load("http://www.stackoverflow.com#container");
});
</script>
Is there a way to do this through Jquery, or is there an alternate solution?
If you can encode the other domain's data in JSON, you can do cross-domain requests using JSONP requests. This of course requires that you are able to change or request a different type of encoding from the other domains so if that's not under your control this approach is not possible.
No, you can't just load up another page (or a piece of it) like that with Javascript. You would need to do it on the backend via PHP, .NET, or some other server-side scripting language, then pass the results to your page.
You can also get the content of the page that you need and parse it with regexp or as above was said Nate B, Write some type of code for example Rss Feed, Pass content with json, create some web service and etc
I want to get a short string hosted on a server where I do not have access to the data as XML, JSON, etc. I am trying to use either .load or .ajax to do this. I want to be able to parse the data into a javascipt array. The entire contents of the remote page is text and I am happy to take all of it and remove what I do not need via a small javascript. I have tried:
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({url:"http://url:8888/data", success:function(result){
$("div").html(result);
}});
});});
</script>
I have two questions.
1- why does this not work?
2- What would be the best way to store the string in a javascript var?
I am sure JQuery is working correctly.
The answer would be to long to post here (really). But look those up:
Same Origin Policy
Padded JSON
If you have no control over the remote site, you have lost - you will not get any data from it by Ajax (which is actually a feature, not a limitation of the technology). One way of circumventing the protection would be to build a proxy that just mirrors the remote service you need to reach and makes it available in the same domain that your main HTML came from.