Http request error when using AJAX.

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Sep 2006
Posts: 68
Reputation: rpjanaka is an unknown quantity at this point 
Solved Threads: 0
rpjanaka rpjanaka is offline Offline
Junior Poster in Training

Http request error when using AJAX.

 
0
  #1
Mar 24th, 2007
Hi all,

I am using AJAX to submit a data from a web page, it is properly working on the local host (when test with the local machine it is ok). When access from another machine the pages are properly lord. But when send data it gives an http request error.

The following is the error message.

pls anyone can help me to find the reason for this...









ERROR
The requested URL could not be retrieved

While trying to process the request:

POST /OmniPreferedRoaming/Omni%20Preferred%20Roamer/Action/add-operator-action.jsp?action=add&opCountry=CAN&opName=can2&opCC=456&opNDC=6325&opMCC=156&opMNC=0036&sid=0.6507964352578459 HTTP/1.1
Host: 172.16.7.49:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Referer: http://172.16.7.49:8080/OmniPrefered...d-operator.jsp
Cookie: JSESSIONID=5692686F83690546771C7B1E7B6B02DF
Proxy-Authorization: Basic cnBqYW5ha2E6YTdoMzEkNW0=
Pragma: no-cache
Cache-Control: no-cache


The following error was encountered:

* Invalid Request

Some aspect of the HTTP Request is invalid. Possible problems:

* Missing or unknown request method
* Missing URL
* Missing HTTP Identifier (HTTP/1.0)
* Request is too large
* Content-Length missing for POST or PUT requests
* Illegal character in hostname; underscores are not allowed
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Http request error when using AJAX.

 
0
  #2
Mar 24th, 2007
Hi,

Could you post some of your code?

The error message is quite detailed.. have you checked those suggestions?
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 68
Reputation: rpjanaka is an unknown quantity at this point 
Solved Threads: 0
rpjanaka rpjanaka is offline Offline
Junior Poster in Training

Re: Http request error when using AJAX.

 
0
  #3
Mar 26th, 2007
function sendRequest(url){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.send(null);
}







function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
}
}



the above are the cord used,



* Missing or unknown request method
* Missing URL
these two could not be happend..

* Request is too large
this is also not be a reason because, the page contain several number of data fields, that is like a normal web page
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Http request error when using AJAX.

 
0
  #4
Mar 27th, 2007
Your code seems ok to me except that it doesn't allow you to send HTTP POSTs the right way.

In a HTTP POST, parameters should be in the HTTP Body, not in the URI. The error message shows parameters are being appended to the URI.

[HTML]
POST /OmniPreferedRoaming/Omni%20Preferred%20Roamer/Action/add-operator-action.jsp?action=add&opCountry=CAN&opName=can2&opCC=456&opNDC=6325&opMCC=156&opMNC=0036&sid=0.6507964352578459 HTTP/1.1
[/HTML]

I don't think it really matters if its in the URI though. You just have to make sure each parameter is urlencoded if you place it in the URI.

Heres an example taken from a larger class/object:

[HTML]/**
* encode passed http vars
*/
fwd_XHR.prototype.encode = function(uri) {
if (encodeURIComponent) {
return encodeURIComponent(uri);
}
if (escape) {
return escape(uri);
}
return uri;
};

/**
* dencode passed http vars
*/
fwd_XHR.prototype.decode = function(uri) {
uri = uri.replace(/\+/g, ' ');

if (decodeURIComponent) {
return decodeURIComponent(uri);
}
if (unescape) {
return unescape(uri);
}
return uri;
};[/HTML]

If you append parameters to the HTTP body in a HTTP POST, you should also specify the Content-Type header as "application/x-www-form-urlencoded". Assuming you want it to be parsed by the server into HTTP POST parameters.

Try something like:


[HTML]function sendPOST(url, data){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
data += "&sid="+encodeURIComponent(Math.random());
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=stateChanged;

xmlHttp.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

// one of the error message suggestions is to include content-length header. so try that...
xmlHttp.setRequestHeader("Content-Length", encodeURIComponent(data.length));

xmlHttp.send(data);
}[/HTML]

Call it like:
[HTML]
// the uri has to be url encoded
url = '/OmniPreferedRoaming/Omni%20Preferred%20Roamer/Action/add-operator-action.jsp';

// so does each param value
data = 'action='+encodeURIComponent('add');
data += '&opCountry='+encodeURIComponent('CAN'); // etc.[/HTML]

Also note I included the content-length header which is suggested in teh error message.



Don't think this is related but in the code:

[HTML]xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.send(null);[/HTML]

you might want to change to:

[HTML]
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.send(null);[/HTML]

(I think there is a problem that arises in one of the browsers where the xmlhttprequest resets the object when you call the "open" method - but that might be in IE)
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 68
Reputation: rpjanaka is an unknown quantity at this point 
Solved Threads: 0
rpjanaka rpjanaka is offline Offline
Junior Poster in Training

Re: Http request error when using AJAX.

 
0
  #5
Mar 28th, 2007
this error is only reported with the Mozilla browser, when it use IE, there is no error and AJAX functions are working properly...

what could be the reason for that...?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Http request error when using AJAX.

 
0
  #6
Mar 28th, 2007
Originally Posted by rpjanaka View Post
this error is only reported with the Mozilla browser, when it use IE, there is no error and AJAX functions are working properly...

what could be the reason for that...?
It may be that Firefox is not sending the HTTP request at all. From the error message it looks like firefox checks against a few rules to see if the HTTP Request is valid, and if it isn't, it gives the error you see...

You may want to try downloading the Firefox Extension called Firebug (if you haven't).

Firebug Dev site: http://www.getfirebug.com/

Once installed an enabled you will be able to view all the HTTP Requests made by firefox. You can filter to view only the "XHR" - XML HTTP Requests.

See if the XHR Request is being sent or not.

BTW: Did you try sending the Content-Length HTTP Request Header?

[HTML]xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");[/HTML]
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 54
Reputation: HazardTW is an unknown quantity at this point 
Solved Threads: 2
HazardTW HazardTW is offline Offline
Junior Poster in Training

Re: Http request error when using AJAX.

 
0
  #7
Sep 11th, 2007
Digital Ether, you saved me from pulling my hair out.

I was trying to work on my website at a different location and Firefox would not properly handle my http requests, even though it works fine at home.

Forgetting that I had Firebug installed at home I couldn't figure out why I could not find the GET and POST data I was looking for.

After reading your post I installed Firebug to see if I could get to the information and solve the problem, but just installing Firebug fixed the problem!

While this is great for me I am concerned about the users of my web app.

Here is the HTTP request object I got from somewhere, I can't remember where. (I am new at this AJAX stuff)
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function HttpClient() { } // HTTPCLIENT CLASS
  2. HttpClient.prototype = {
  3. // type GET,POST passed to open
  4. requestType:'GET',
  5. // when set to true, async calls are made
  6. isAsync:true,
  7. // where an XMLHttpRequest instance is stored
  8. xmlhttp:false,
  9. // what is called when a successful async call is made
  10. callback:false,
  11. // what is called when send is called on XMLHttpRequest
  12. // set your own function to onSend to have a custom loading
  13. // effect
  14. onSend:function()
  15. {
  16. //document.getElementById('HttpClientStatus').style.display = 'block';
  17. },
  18.  
  19. // what is called when readyState 4 is reached, this is
  20. // called before your callback
  21. onload:function()
  22. {
  23. //document.getElementById('HttpClientStatus').style.display = 'none';
  24. },
  25.  
  26. // what is called when an http error happens
  27. onError:function(error)
  28. {
  29. alert(error);
  30. },
  31.  
  32. // method to initialize an xmlhttpclient
  33. init:function()
  34. {
  35. try
  36. {
  37. // Mozilla / Safari
  38. this.xmlhttp = new XMLHttpRequest();
  39. }
  40. catch (e)
  41. {
  42. // IE
  43. var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
  44. 'MSXML2.XMLHTTP.4.0',
  45. 'MSXML2.XMLHTTP.3.0',
  46. 'MSXML2.XMLHTTP',
  47. 'Microsoft.XMLHTTP');
  48. var success = false;
  49. for (var i=0;i < XMLHTTP_IDS.length && !success; i++)
  50. {
  51. try
  52. {
  53. this.xmlhttp = new ActiveXObject
  54. (XMLHTTP_IDS[i]);
  55. success = true;
  56. }
  57. catch (e)
  58. {}
  59. }
  60. if (!success)
  61. {
  62. this.onError('Unable to create XMLHttpRequest.');
  63. }
  64. }
  65. },
  66. // method to make a page request
  67. // @param string url The page to make the request to
  68. // @param string payload What you're sending if this is a POST
  69. // request
  70. makeRequest: function(url,payload)
  71. {
  72. if (!this.xmlhttp)
  73. {
  74. this.init();
  75. }
  76. this.xmlhttp.open(this.requestType,url,this.isAsync);
  77.  
  78. // set onreadystatechange here since it will be reset after a
  79. //completed call in Mozilla
  80. var self = this;
  81. this.xmlhttp.onreadystatechange = function()
  82. {
  83. self._readyStateChangeCallback();
  84. }
  85. if (this.requestType == "POST")
  86. {
  87. this.xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  88. this.xmlhttp.setRequestHeader("Content-length", payload.length);
  89. this.xmlhttp.setRequestHeader("Connection", "close");
  90. }
  91.  
  92. this.xmlhttp.send(payload);
  93.  
  94. if (!this.isAsync)
  95. {
  96. return this.xmlhttp.responseXML;
  97. }
  98. },
  99.  
  100. // internal method used to handle ready state changes
  101. _readyStateChangeCallback:function()
  102. {
  103. switch(this.xmlhttp.readyState)
  104. {
  105. case 2:
  106. this.onSend();
  107. break;
  108. case 4:
  109. this.onload();
  110. if (this.xmlhttp.status == 200)
  111. {
  112. this.callback(this.xmlhttp.responseXML);
  113. }
  114. else
  115. {
  116. this.onError('HTTP Error Making Request: ' + '[' + this.xmlhttp.status + ']' + ' ' + this.xmlhttp.statusText);
  117. }
  118. break;
  119. }
  120. }
  121. }
And the offending call:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function set_dynamic_globals()
  2. {
  3. client.requestType = "GET";
  4.  
  5. client.callback = function(XMLresult)
  6. {
  7.  
  8. if (XMLresult.getElementsByTagName("status"))
  9. {
  10. http_status = XMLresult.getElementsByTagName("status")[0].childNodes[0].nodeValue;
  11.  
  12. }else{
  13. http_status = "FAILED";
  14. return;
  15. }
  16. if (http_status == "SUCCESS"){
  17. // bunch of code that reads the values from the XML doc
  18. }
  19. }
  20. client.makeRequest(url,null);
  21. return;
  22. }

This call does not actually send anything, the target is a PHP created XML doc that contains values read from a database.
Do you see anything wrong with this that would cause Firefox (without Firebug) to fail while IE 6 and IE 7 worked fine?

Much thanks!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 54
Reputation: HazardTW is an unknown quantity at this point 
Solved Threads: 2
HazardTW HazardTW is offline Offline
Junior Poster in Training

Re: Http request error when using AJAX.

 
0
  #8
Sep 12th, 2007
Just wanted to add that when I got home I tested on another PC (other than my main PC) running Linux with Firefox 2.0.0.6 and had the same problem with the http requests, I installed and enabled Firebug and it all started working.

So I am kind of curious what Firebug is doing to Firefox that is making it work where it otherwise would not.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Http request error when using AJAX.

 
0
  #9
Sep 12th, 2007
It looks ok to me. That would be odd that Firebug actually makes a difference.

Do you get an error message with that last bit of code or does it just not send a XHR?

I assume url and client are globals. What is the value of url?

You should put some alert()'s into the code to see how far the XHR goes and than quits...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 54
Reputation: HazardTW is an unknown quantity at this point 
Solved Threads: 2
HazardTW HazardTW is offline Offline
Junior Poster in Training

Re: Http request error when using AJAX.

 
0
  #10
Sep 12th, 2007
Yes client and url are global.

I did put some alerts in and got as deep as the call to the internal onreadystatechange method.
I could get no errors generated from the class either, best I can tell is it got stuck between ready states???

Not sure if it has any bearing on this, but outside the prototype, where an instance of HttpClient is created:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var client = new HttpClient();
I set client.isAsync to false to halt the js execution until I get my results back.

This app is just for office use, not public use, but I know some of the users do use Firefox so I added an alert specifically for this condition telling them to install Firebug... LOL Oh, I was nice and gave them to url to the download too
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC