views:

45

answers:

3

This is my ajax code,

For example URL 1 : www.text1.com and URL 2 : www.text2.com/check.asp

Here i wand to post data from text1.com to text2.com....

Is it possible?

<script type="text/javascript">
function ajaxFunctionSearch() {
    var xmlHttp;

    try {
        xmlHttp = new XMLHttpRequest();
    }
    catch(e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            document.getElementById("SXML").value = xmlHttp.responseText;
        }
    }
    var params = "CountryID=" + document.getElementById("DEMOCNY").value
    xmlHttp.open("POST", "http://text2.com/Check.asp", true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);

}
</script>
+1  A: 

no because of the same origin policy, in short you javascript can talk only to server on the same domain has the page it was got from.

You have to make some server script to also do the post to your www.test2.com.

you can take a look there how to build a proxy in C#, basically you would have to play with HttpWebRequest to forward the ajax call to www.test2.com.

Also if both server are in the cabinet you probably have better to do the DB insert or any kind of processing directly if you can.

RageZ
What type of server script.., can u help me for that.. because , these two websites are in my control
Alex
what server language are you using ?
RageZ
Servar langauge is Classic ASP
Alex
ha classic ASP, that's difficult, you don't have access to an PHP/ASP.net install ?
RageZ
asp.net is ok for me
Alex
@Alex: I have a link with some article how to do it, have a good reading.
RageZ
A: 

No - a security restriction of JavaScript is that it is unable to communicate with a domain different from the one that the page was served from.

If your page is served from www.text1.com, then you will only be able to make requests to other pages from the domain www.text1.com.

Kragen
A: 

Like the previous answerers said, it is in principle not possible. But recently techniques are developed to allow cross-domain requests in certain circumstances.

Google for CORS or look here: hacks.mozilla.org. IE8 also incorporates an XDomainRequest object, but I never used this.

But they are still very alpha and not what anyone would call cross-browser technologies.

Boldewyn