shobhitjain Posted November 9, 2009 Share Posted November 9, 2009 here is the code,it is not working,,please tell why<html><head><script type="text/javascript">function xmlhttpPost(strURL) {//alert(strURL);var xmlHttpReq = false; var self = this;if (window.XMLHttpRequest) { self.xmlHttpReq = new XMLHttpRequest(); } // IE else if (window.ActiveXObject) { self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } self.xmlHttpReq.open('POST', strURL, true); self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); self.xmlHttpReq.onreadystatechange = function() { if (self.xmlHttpReq.readyState == 4) { alert(self.xmlHttpReq.responseText); } } self.xmlHttpReq.send(getquerystring());}function getquerystring() {var params = "q_id=2&g_id=166829";return params;}</script> </head><body><input type="button" value="submit" onclick="xmlhttpPost('http://admin.mysite.com/research/');"></body></html> Link to comment Share on other sites More sharing options...
Ingolme Posted November 9, 2009 Share Posted November 9, 2009 I don't know if using if() works well in Internet Explorer. Last time I tried, Internet Explorer accepts that there's an XMLHttpRequest() object but it can't use it. This is why I use try / catch. try { self.xmlHttpReq = new XMLHttpRequest();} catch(e) { try { self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { alert("AJAX not supported"); }} How exactly is it not working? Have you checked the error console?Edit: By the way, AJAX only works on pages within the same subdomain. If you're trying to access another domain it will not allow you to. Link to comment Share on other sites More sharing options...
shobhitjain Posted November 9, 2009 Author Share Posted November 9, 2009 i am trying to run it on firefoxI got no response after click.just a blank alert box. Link to comment Share on other sites More sharing options...
shobhitjain Posted November 9, 2009 Author Share Posted November 9, 2009 yes my page is on localhost and the site is hosted on another server,so you mean to say that this script won't work Link to comment Share on other sites More sharing options...
Synook Posted November 9, 2009 Share Posted November 9, 2009 yes my page is on localhost and the site is hosted on another server,so you mean to say that this script won't workYes, it won't work - as I mentioned in another thread, you need some sort of server-side script to proxy the request. Such a PHP script may appear thus:<?php echo file_get_contents($_GET['url']);?> Link to comment Share on other sites More sharing options...
shobhitjain Posted November 9, 2009 Author Share Posted November 9, 2009 yes i know sir,but i have tried all the cross domain script,can you provide me a working script on which i can rely,i just need to post some data on server via url.site is built in perlplease help sir, Link to comment Share on other sites More sharing options...
jeffman Posted November 9, 2009 Share Posted November 9, 2009 If you got an empty alert box, that means your server sent a response. But the server script may have run incorrectly. Please post the server script. Link to comment Share on other sites More sharing options...
shobhitjain Posted November 9, 2009 Author Share Posted November 9, 2009 Sir i dont know about the server script ,because it is not my concern, i have to just pass values to at some server which in turn saves those values Link to comment Share on other sites More sharing options...
justsomeguy Posted November 9, 2009 Share Posted November 9, 2009 You need a server script on your server. We've told you before that Javascript cannot send a request to another domain. It doesn't matter what that Javascript looks like, all Javascript cannot access a different domain. There's no such thing as a "cross-domain ajax", ajax is not cross-domain. Ajax requests always go to the same domain. So you need a server script on your local server, the same server your Javascript is on, and your Javascript code needs to send a request to that script instead of the remote site. The request to the local script needs to tell it the URL of the remote site, and the server script will load the remote site and give it back to Javascript. It doesn't matter that the remote server is using Perl, you can use any language that your local server supports to set up a script to redirect the ajax request.Do you understand everything I just said? You need to understand that if you're going to get this to work. Link to comment Share on other sites More sharing options...
jeffman Posted November 9, 2009 Share Posted November 9, 2009 It would be easier if this could be a GET request, because we could stick everything in a URL and just forward that to the destination server.But, going by the original script, Shobit needs to make a POST request. To create a proxy script on the AJAX server, I believe we will have to use some cURL library functions.Anyone see a simpler way? Link to comment Share on other sites More sharing options...
Synook Posted November 9, 2009 Share Posted November 9, 2009 I suppose you could GET to the proxy script, which converts the request into a POST before sending it on. POSTing with cURL isn't too hard, you can even just give it the $_GET array and it will do everything else for you. curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $_GET); Link to comment Share on other sites More sharing options...
jeffman Posted November 9, 2009 Share Posted November 9, 2009 Exactly. Just pass it thru and let the destination script handle the validation.What if the destination script checks the origin of the POST request? What would they check? Could you spoof that with the REFERER option, or would it be something else? (I haven't used cURL very much and never had a need to spoof.) Link to comment Share on other sites More sharing options...
shobhitjain Posted November 10, 2009 Author Share Posted November 10, 2009 yes sir ,I understood,what you are saying, I have never used cURL. Link to comment Share on other sites More sharing options...
Synook Posted November 10, 2009 Share Posted November 10, 2009 Well, if you want to make a server-side proxy, then it's time to start learning (although, it's always time to learn). Link to comment Share on other sites More sharing options...
shobhitjain Posted November 10, 2009 Author Share Posted November 10, 2009 Thanks.I Agree and respect you. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.