473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

alert box on ajax communication

Hallo,

I seem to have a sneaky little problem in my ajax-based function. The function is supposed to check at the server if the given username/password-combination is valid. On server-side everything is working correctly (Output is either "OK" or "Error") but within the js-application I always receive an alert-box stating "undefined" in the very end.

Could you please take a look at my code and give me a hint where the problem is?

Expand|Select|Wrap|Line Numbers
  1. ajaxRequest = function(u,f,m,b,h,s)
  2. {
  3.     alert ('ajaxRequest');
  4.     this.url = u;
  5.     this.wState = f || function() { };
  6.     this.method = m || "GET";
  7.     this.body = b || null;
  8.     this.headers = h || false;
  9.     this.sync = s || true;
  10.     this.abortReq = false;
  11.  
  12.     this.req = (window.XMLHttpRequest) //Gecko-Browser?
  13.         ?
  14.         new XMLHttpRequest() //Gecko-Browser!
  15.         :
  16.         ((window.ActiveXObject) //Internet Explorer?
  17.         ?
  18.         new ActiveXObject("Microsoft.XMLHTTP") //Internet Explorer!
  19.         :
  20.         false //some Error occured
  21.         );
  22.     this.doRequest = function() //function to perform the request
  23.     {
  24.         alert('doRequest');
  25.         this.req.open(this.method,this.url,this.sync); //Open connection
  26.         if (this.headers) //additional headers given?
  27.         {
  28.             for (var i=0; i<this.headers.length; i+=2)
  29.             {
  30.                 this.req.setRequestHeader(
  31.                 this.headers[i],this.headers[i+1] //construct keyvalue pairs for the header
  32.                 );
  33.             }
  34.         }
  35.         this.req.onreadystatechange = this.wState; //function-call
  36.         (!this.abortReq) ? this.req.send(this.body) : this.req.abort();
  37.         //send request if not aborted somehow
  38.     }
  39. }
  40.  
  41. /**
  42. *   function to check whether the connection can
  43. *   be established for given username and password
  44. *
  45. *   @param: string username Username
  46. *   @param: string password Password
  47. */
  48. var checkConnection = function (username, password) {
  49.     xmlhttp = new ajaxRequest(
  50.         'http://url*****',
  51.         function()
  52.         {
  53.             alert('callback-function');
  54.             var r = xmlhttp.req;
  55.             if (r.readyState==4) //transfer complete
  56.             {
  57.                 alert('readystate!')
  58.                 document.getElementById('test-label').value =
  59.                 (r.status == 200) //insert Data in DOM
  60.                     ? r.responseText : r.status+"ERROR"; //Error occured
  61.                 alert ('responsetext inserted');
  62.             }
  63.         },
  64.         'POST',                                     //type
  65.         '&username='+username+'&password='+password, //query
  66.         ["Content-Type","application/x-www-form-urlencoded"]    //header
  67.     );
  68.  
  69.     xmlhttp.doRequest();
  70. }
  71.  
When I perform the query the debug messages appear in following order:
1.) ajaxRequest
2.) doRequest
3.) callback-function
4.) callback-function
5.) callback-function
6.) readystate!
7.) responsetext inserted (*it actually is updated*)
8.) callback-function
9.) readystate!
10.) responsetext inserted
11.) undefined (appears always)

My question is: Why is the callback-function called again (debug no. 8) and why do I always receive the alert-box "undefined", whithout calling any alert?!

Thanks for your help in advance!

Greets,
mischie
Aug 25 '11 #1
0 1257

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Gustavo Amigo | last post by:
Hi, i´ve started a project on sourceforge. It´s called Ajax Pages, it´s a scripting template engine that enables rapid development of Ajax Web-based Applications. I would like you guys to take a...
4
by: rvandervort | last post by:
I'm having a real problem here. I have a JS function: getExpenseDetails() it's a asynch call to a php script behind the scenes which returns XML formatted expense details. in my onreadystate...
2
by: needin4mation | last post by:
What is .NET remoting versus Ajax? Any links are appreciated. I didn't find a straight forward answer. Thanks for any help.
1
by: conspireagainst | last post by:
Is there any possible way (I'm using prototype framework) to glean what the name of the container element to be updated (using Ajax.Updater) is? I have appended at the bottom of prototype.js a...
3
JamieHowarth0
by: JamieHowarth0 | last post by:
Hi folks, I have a bit of a headache. I've finally added all the nice finishing touches to my own website (static only with a bit of DHTML). Now I've just converted the whole thing to AJAX with a...
1
by: bizt | last post by:
Hi, I am having my first real attempt at an ajax class as so far Ive managed to build one that, once instatiated, will allow me to define which function it will call on completion then retrieves...
10
dmjpro
by: dmjpro | last post by:
function synchronous_ajax(){ var ajax = null; if(typeof ActiveXObject!='undefined') ajax = new ActiveXObject("Microsoft.XMLHTTP"); else if(typeof XMLHttpRequest!='undefined') ajax = new...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
7
by: mike57 | last post by:
The minimal AJAX script below works in Firefox, but not in IE, Opera, or Chrome. I could use some suggestions or referrals to resources that will help me get the script working in other browsers. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.