Ajax cross-sub-domain requests? - javascript

Is there a way to make an Ajax request to
s3-ap-southeast-1.s3.amazonaws.com (to S3 API)
from
s3.amazonaws.com
(from where a JavaScript app that is hosted at)?

You cannot do cross-domain ajax requests. That includes subdomains. However, it is possible to use JSONP.

yes, you can cross domain ajax calls, check cross-origin resource sharing:
http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

Cross domain ajax requests are forbidden by protocol. And yes, subdomains too.
Read here: http://www.ajax-cross-domain.com/ It might help;

I know this is an old post, I provided a detailed example for cross domain ajax request using JSONP, hopefully it helps those who is in trouble:
http://www.shanison.com/2012/05/11/cross-domain-ajax-request/

shazmo said this in a earlier post:
Cross domain is entirely a different
subject. But cross sub-domain is
relatively easy.
More info here:
http://www.tomhoppe.com/index.php/2008/03/cross-sub-domain-javascript-ajax-iframe-etc/

Shameless plug, but this may help:
http://alexn.org/blog/2011/03/24/cross-domain-requests.html

I guess I found the link that #Patrick had posted and it had become broken
http://hoppeweb.blogspot.com/2008/03/cross-sub-domain-javascript-ajax-iframe.html
to avoid happening this again I will just try to re-post it)
The idea is setting up an iframe html on one domain and then calling that iframe from the page on the other subdomain. Both parent page and the iframe inside it should have the same document.domain.
document.domain = "example.com"
once done, those two pages act like they are on the same domain.
the rest, just copy-pasted ((
For example, for pulling in text, setup your page on
www.yourdomain.com and set document.domain to yourdomain.com. If you
are trying to pull in an html page using Ajax from img.yourdomain.com,
setup a page that, will become the iframe, to do the ajax pull. After
that pull is complete set the document.domain to yourdomain.com. In
your page on www. create an iframe which has the src set to your page
on img. Since document.domain is set, any functions on the parent page
are available to be called via the iframe. Lets say you want to put
your newly "ajaxed" html into a div on the parent page, you can do
that via "parent.getElementById('yourDivName').innerHTML =
Response.Text".
If you are pulling in XML, you can setup the page/iframe relationship
the same as above. That iframe will make the ajax call to the XML on
img.yourdomain.com and do something with it, lets say turn it into an
array. Once that is completed, set the document.domain on the iframe
page. At this point, the parent page can access that array on its
iframe via "iframeName.arrayName". Alternatively you can have an array
read on the parent page for this information and pass it to the parent
from the iframe via "parent.arrayName = iframeArray".
originally by #Tom Hoppe

Related

Access info from outer page from within embedded iframe

I have written a page, served from the domain banana.example.com. I own that domain. The purpose of the page is for it to be embedded in pages from mango.example.com, which I don't own. (Same "big domain," but not exactly the same domain.) This works fine. I give the people at Mango the URL and they can embed my Banana page in an iframe on their site.
I would like for my page to display slightly different content depending on exactly which page on mango.example.com it is being embedded.
If I write:
console.log(window.parent.window.location);
I get the full URL printed to the console as a Location object, but I can't access any of that object's properties (e.g., href, pathname) or use either implied or explicit methods to stringify the location object.
Is there any way that I can get information about the page that my page is being embedded in? URL, title, anything? (For the purposes of this question, please assume that I only have authority over banana.example.com and can't ask the folks at mango.example.com to do anything.)
It is because of the browser's CORS (Cross Origin Resource Sharing). A solution for this problem would be to post a message to the outer page requesting for information, which, in turn would post a response to your iframe which will have a handler for that. Read about postMessage here. Note that in case you are not controlling the outer page you will not be able to solve the problem like this. You will need to convince the owner of Mango to receive your message where the iframe is requesting for information and to use postMessage to respond to it.

How to wait for an HTML page and get result when page complitely created?

I have a page (page1.html) and I want to send an ajax to page2.html (http://m-kermani.github.io/getapp.html) and page2.html has an iframe that made by javascript
I made it by javaScript because I need to send a parameter to the page3
In page1.html I have:
$.get('https://m-kermani.github.io/getapp.html', function (data) {
alert(data);
});
and I just need the iframe content but beacuse it made by JavaScript I can't get it and that did not created! (This is the way JavaScirpt is)
I need to send ajax request and get the iframe content because I need an https domain for some reasons that GitHub.io is!
No I need to know is there anyway I can get the content of the iframe from GitHub page?
Is there any other way I can direly just have GitHub page and give the parameter to it and can get the content of the page3 (not using server side language)?
And suggestion about what can I do?
Sounds like you're trying to circumcent the same-origin policy. Unless the API you're trying to access specifically supports a way to do it (CORS, JSONP, etc), you can't do it. You should read the documentation of the API you're trying to access to see if they support accessing it from the client side.
An Ajax request is just a request for a resource. It just gets whatever the server is going to send. It doesn't automatically render the HTML and fetch dependant resources.
If you want the content of a frame, then you have to request the URL for the frame instead of the URL for the page with the <iframe> tag in it.
(The Same Origin Policy will still apply).

How can I pass my referrer from current page to iframe without using php?

I have an iframe in mysite.com/folder/file.php
The iframe is an html page from a subdomain and I want to block all referers except the current page it's embeded in (mysite.com/folder/file.php)
What's the best approach to this, using javascript ?
I tried to define rules in Nginx but the problem is the headers always show subdomain.com as the referer instead of showing current page.
The nginx approach could work. There is an nginx module which can filter by referer.
But the docs for that module state that the header is unreliable. It can be easily modified to show incorrect information by hackers.
But the DOM in the iframe'd html page should give you access to the 'parent' object.
That object could then be used to obtain information from the parent page like url or even some custom data you could set in the parent page.
I am not a javascript expert but I think this is the better approach.

Loading a div from a different domain

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

Asynchronous cross-domain POST request via JavaScript?

I could just create a form and use that to do a POST request to any site, thing is the FORM method isn't asynchronous, I need to know when the page has finished loading. I tried messing around with this using an iframe with a form inside, but no success.
Any ideas?
EDIT
unfortunately I have no control over the response data, it varies from XML, json to simple text.
You can capture the onload event of an iframe. Target your form to the iframe and listen for the onload. You will not be able to access the contents of the iframe though, just the event.
Try something like this:
<iframe id='RS' name='RS' src='about:blank' onload='loaded()'></iframe>
<form action='wherever.php' target='RS' method='POST'>...</form>
script block:
var loadComplete = 0
function loaded() {
//avoid first onload
if(loadComplete==0) {
loadComplete=1
return()
}
alert("form has loaded")
}
IF you want to make cross domain requests you should either made a JSON call or use a serverside proxy. A serverside proxy is easy to set up, not sure why people avoid it so much. Set up rules in it so people can not use the proxy to request other things.
If the data returned from the cross domain post is JSON, then you can dynamically add a script tag pointing to the URI that returns the data. The browser will load that "script" which then you can access from other javascript.
YUI3's IO object offers cross-domain requests, however it does so using a small Flash control it embeds on the page.
While there is work going into secure cross-domain requests from JavaScript, at this time, you need to use a plugin like Flash or Silverlight as a bridge with which to make the request.
You can't do anything cross-domain using javascript. You'd have to use a backend language like PHP or asp or something.

Categories