ASP/Ajax application giving "Length Required" error on IIS6/Firefox
I had an application written in ASP (VB Script) with Ajax, and it worked on IIS5 with all browsers. Now, after moving to IIS6, it only works on IE, and gives the error on firefox.
Here is the code:
function getresult(str){ xmlHttp=GetXmlHttpObject();if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } var url="result1.asp";url=url+"?fname="+document.form1.fname.value+"&lname="+ document.form1.lname.value+"&ext="+ document.form1.ext.value;url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("POST",url,true);xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xmlHttp.send(null);}function goletter(vs){var nam = this.lt.value;window.location.href = "letresult.asp?letv=a&letsort="+nam;}function godept1(URL_List){xmlHttp=GetXmlHttpObject();if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } var url1 = document.form1.dept.options[document.form1.dept.selectedIndex].value;var space = / /;var fl=url1.length;for(f=1;f<fl;f++) url1=url1.replace(space, "+"); url = "result1.asp?deptn="+url1;url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("POST",url,true);xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xmlHttp.send(null);}
When using POST, you need to send the parameters via the send() method, not via the url. You also need to specify a Content-length header that contains the length of the parameters. If you are going to pass the parameters via the url, then you need to use GET instead
function getresult(str){ xmlHttp=GetXmlHttpObject();if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } var url="result1.asp";var params = "fname="+document.form1.fname.value+"&lname="+ document.form1.lname.value+"&ext="+ document.form1.ext.value;params=params+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("POST",url,true);xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xmlHttp.setRequestHeader("Content-Length", params.length);xmlHttp.send(params);}function goletter(vs){var nam = this.lt.value;window.location.href = "letresult.asp?letv=a&letsort="+nam;}function godept1(URL_List){xmlHttp=GetXmlHttpObject();if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } var url1 = document.form1.dept.options[document.form1.dept.selectedIndex].value;var space = / /;var fl=url1.length;for(f=1;f<fl;f++) url1=url1.replace(space, "+"); url = "result1.asp";var params = "depth="+url1;params=params+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("POST",url,true);xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xmlHttp.setRequestHeader("Content-length", params.length);xmlHttp.send(params);}
Change Content-type and Content-length to Content-Type and Content-Length respectively. If problems continue post the rest of your code OR better yet, a url to your page.
Wait, you have two function making ajax requests - godept1() and getresult(). Are both giving the same error? On what I posted one of them has Content-Length and the other has Content-length (lowercase L for length), so I would expect at least one of them to work. Whichever one works, change the non-working one accordingly.
kalmen
ASKER
Neither of them are working. I'll run through the code again and possibly paste it here for you. Tomorrow :)
I came to realize that FireFox fails to send the content length to IE, which could be a bug?
because when I add: xmlHttp.setRequestHeader("Content-Length", params.length); It doesn't get sent to the server at all.
Would you agree? Of course, I wouldn't have came to this realization without your help.
>>...Would you agree?
I cannot say I do because this makes NO sense:
"I came to realize that FireFox fails to send the content length to IE"
IE and Firefox are both browsers, and both communicate with a Web Server. IE does NOT communicate with Firefox or vice versa. So they are NOT sending anything to each other.
kalmen
ASKER
Ouch... Sorry, I meant to say that firefox doesn't send the content-length to IIS6 (NOT IE, my bad!). After doing research, I came to realize that firefox/ajax/iis6 can only use GET and not POST because of this. Does this make sense to you?
Morcalavin
I used II6 and POST all the time within firefox. It supports ajax just fine.
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", parameters.length);
xmlhttp.setRequestHeader("Connection", "close"); //keep-alive needed if using ntml authentication
Also, you should be using escapeURIComponent(value) instead of escape(value) when you build our paramenter list.
>>After doing research, I came to realize that firefox/ajax/iis6 can only use GET and not POST because of this.
Sorry, still do not agree. IF anything the issue/bug with be with your version of FF. I've implemented ajax/post successfully with no problem with FF 2.x and IE6 posting to IIS6. IF the issue is in fact the browser (as opposed to the server - which I know is not the issue), then at least for the time being you will need to temporatily switch to "GET" until the issue is resolved.
kalmen
ASKER
If you've successfully tested it, then it must work. Let me double check my code. I'll try escapeURIComponent(value) when building my parameter list. I'll keep you posted and thank you for your support.
kalmen
ASKER
You are right. I realized that my code is correct, but when calling the javascript functions I wasn't passing an object, so that is all what was to it.
I was mislead by many posts on the internet that FF is not working with IIS6, and thought this could be the issue. But thanks to your expertise, things are all in shape.
Open in new window