Forum Moderators: open

Message Too Old, No Replies

subdomain with main domain iframe and ajax problem

using ajax in the subdomain and js between iframe and main page

         

proper_bo

4:09 pm on Jun 13, 2006 (gmt 0)

10+ Year Member



I have a page at [subdomain.example.com...]
part of the page is an iframe http://example.com/iframe.html
and after the iframe is a form

the iframe makes an ajax call to another file http://example.com/ajax.xml

when the submit button on the main page is clicked it calls a function in the iframe and retrieves a value to put in a hidden field in the form.
to get this to work I have to set document.domain="example.com"; in both the main page and the iframe. correct?

well because I have to do that to make them talk to each other the ajax call will not work. Because I am calling an xml page I cannot set javascript document.domain there.

Is there any way you can think of that will allow both the ajax call and the passing of values from iframe on main domain to subdomain to work?

Thank you.

ps. title should read:
using ajax in the iframe and js between iframe and main page

jshanman

4:49 pm on Jun 13, 2006 (gmt 0)

10+ Year Member



Does your form NEED to use POST?

If not then you could use the dynamic scripting technique to bypass the domain issue. But to use this, you need to use the GET method for your form...

- JS

proper_bo

7:40 am on Jun 14, 2006 (gmt 0)

10+ Year Member



It doesn't have to use POST, there are no files involved. You mean submit it in the background and then redirect the page?

Another option I have (although it's not ideal) is to stick the whole form into the iframe. This would mean the subdomain page was only used for url purposes.

jshanman

12:45 pm on Jun 14, 2006 (gmt 0)

10+ Year Member



If you don't need post, then consider this:

JS:
var js = document.createElement("script");
js.type="text/javascript";
var d = new Date():
var q = encodeURIComponent("some query");
js.src = "update.php?q="+q+"&d="+d.valueOf();
document.appendChild(js);

PHP: (or any other server side language)
$q = $_GET{'q'};
#run some query with $q
$stored result as HTML output or Javascript
echo "newData('HTML Output');".$StoredJavascript;

Now this script can be called as many times as you want. You can send form data via GET, insert/update stuff, then return a success or error message. This will all work accross any domain. Whatever script your server page generates is run right away. It's speed is comparable to the XmlHTTPRequest object.

- JS

proper_bo

6:53 am on Jun 15, 2006 (gmt 0)

10+ Year Member



Thats very clever but how do you get the response from the php page (error or data) with the javascript?

For now I just stuck the form in the iframe but it isn't ideal.

jshanman

1:04 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



You can't tell if you get any response other then "200 OK". And you can send the returned data to an existing function.

As soon as that function is run with the new data as the argument, you can do what you want with it (like onreadystatechange).

- JS