Jump to content

Please Tell Me The Error


shobhitjain

Recommended Posts

Can anyone please tell me the error in this code Please <html><head><script language="javascript">function xmlrequest(url) {//alert(url);var http = new XMLHttpRequest();http.overrideMimeType("text/xml");var params = "q_id=2&g_id=166829";http.open("POST", url, true);//Send the proper header information along with the requesthttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");http.setRequestHeader("Content-length", params.length);http.setRequestHeader("Connection", "close");http.onreadystatechange = function() { //Call a function when the state changes.if(http.readyState == 4 && http.status == 200) {alert(http.responseText); } }http.send(params); }</script></head><body><input type="button" value="submit" onclick="xmlrequest('http://mysite.com/research/');"></body></html>

Link to comment
Share on other sites

You've declared your http request object as local to the xmlrequest() function. When the function terminates, the object is destroyed. The easiest solution is to remove the var keyword from the first statement. Even better if you also declare http as a var OUTSIDE the function, in the script's global space. You could do that anywhere in the script, but it is typical to do it right before the function, so that readers of the code (including you) will notice that it really is a global variable. That would look like this:var http;function xmlrequest(url){http = new XMLHttpRequest();// etc.

Link to comment
Share on other sites

First, that code will not run on IE, you're not creating the ajax object in a way that will work in IE6 or IE7. So, if you're testing with those browsers it's not going to run. Second, you're overriding the mime type returned by the server to be text/xml, but you're trying to access the response in the responseText property. If you're going to override the mime type to XML then the response will probably be in the responseXML property instead of responseText. Unless the server is actually returning XML data, it's probably best not to override the mime type.

Link to comment
Share on other sites

Now the code is<html><head><script type="text/javascript">var http;function xmlrequest(url) {//alert(url);http = new XMLHttpRequest();var params = "q_id=2&g_id=166829";http.open("POST", url, true);//Send the proper header information along with the requesthttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");http.setRequestHeader("Content-length", params.length);http.setRequestHeader("Connection", "close");http.onreadystatechange = function() { //Call a function when the state changes.if(http.readyState == 4 && http.status == 200) {alert(http.responseText); } }http.send(params); }</script></head><body><input type="button" value="submit" onclick="xmlrequest('http://yoursite.com/research/');"></body></html>

Link to comment
Share on other sites

Install Firebug for Firefox if you haven't already, there's a link in my signature to download it. Once it's installed, open it and click on the Net tab, and then run your code. You should see the ajax request appear in the Net tab. If the request is red there's a problem with the response. Your code will only run if the response code was 200, if it's 404 or 500 or anything else your code won't do the alert. But you can at least use Firebug to look at the request, see which headers were sent, look at the post data, and see the response from the server.

Link to comment
Share on other sites

Request headersDate Fri, 06 Nov 2009 18:27:38 GMTServer Apache/2.2.9 (Fedora)X-Powered-By PHP/5.2.6Content-Length 632Connection closeContent-Type text/html; charset=UTF-8Request HeadersHost yoursite.comUser-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language en-us,en;q=0.5Accept-Encoding gzip,deflateAccept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7Keep-Alive 300Connection keep-aliveOrigin nullAccess-Control-Request-Method POSTResponse HeadersDate Fri, 06 Nov 2009 18:24:55 GMTServer Apache/2.2.9 (Fedora)X-Powered-By PHP/5.2.6Content-Length 632Connection closeContent-Type text/html; charset=UTF-8200 OkThis is what i got..

Link to comment
Share on other sites

You can use console.log to print debug information to the Firebug console, which will show up on the Console tab. Add some debugging stuff to the readystate function to see if it ever finishes:

http.onreadystatechange = function() { //Call a function when the state changes.  console.log('readyState: ' + http.readyState);  console.log('status: ' + httpstatus);...

You should see that show up on the Console tab.

Link to comment
Share on other sites

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: file:///C:/Documents%20and%20Settings/Saurabh%20Jain/Desktop/as.html :: xmlrequest :: line 34" data: no]Line 0i got this error n line 27http.send(params);

Link to comment
Share on other sites

You're probably trying to send the request to a different server. Ajax requests can only go to the same server that the web page is on. It also looks like you're just double-clicking the HTML file to open it, you might have more success if you put it on a real web server and accessed it using HTTP.

Link to comment
Share on other sites

yes you are right ,i was double clicking it,now when i put that html on web server,an error occuredi got this thinguncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://localhost/as.html :: xmlrequest :: line 34" data: no]Line 0readyState: 1readyState: 2status: 0readyState: 4status: 0readyState: 1readyState: 2status: 0readyState: 4status: 0log: readyState: 1log: readyState: 2status: 0

Link to comment
Share on other sites

You have to use a server-side proxy to fetch the information, then request the page from the proxy script.

Link to comment
Share on other sites

You can use jQuery or YUI if you want, all of them just use ajax though. If you want to fetch a remote page using ajax you need to have Javascript send a request to a PHP script on your server and tell it what page to get. The PHP script is allowed to get a remote page, so the PHP script would get the remote page and return it back to the Javascript. That's what Synook meant by a proxy script.

Link to comment
Share on other sites

Sir, i took the reference for jquery for making post,But i found it with php everywhere, Can it be done via javascript.Can you please suggest some links where i can study this thing.I have absolutely no idea about it.So i need to study,pleaseRegards Shobhit

Link to comment
Share on other sites

Are you having problems?Whoops, for some reason I didn't notice this was page two.

Link to comment
Share on other sites

Here is what i made using jquery: <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $.ajax({ type: "POST", url: "http://yoursite.com/research", data: "q_id=2&q_id=166829", success: function(msg){ alert( "Data Saved: " + msg ); } }); </script> </head> <body> </body> </html>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...