Jump to content

Code Is Not Working,please Check


shobhitjain

Recommended Posts

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

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

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
Yes, 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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...