Jump to content

Intermitent Ajax Problem


A1Dan75

Recommended Posts

Hi,

 

I'm using ajax to save data to a mysql database as per the following code, and experiencing intermitent problems:

 

function save()

{

  xmlhttp=GetXmlHttpObject();

  var url="save.php";

  var params="&id="+curRouteID;

  var params=params+"&name="+document.getElementById('rName').value;

  url=url+"?sid="+Math.random();

  url=url+params;

  xmlhttp.onreadystatechange=stateChangedsave;

  xmlhttp.open("GET",url,true);

  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

  xmlhttp.setRequestHeader("Content-length", params.length);

  xmlhttp.setRequestHeader("Connection", "close");

  xmlhttp.send(params);

}

 

function stateChangedsave()

{

  if (xmlhttp.readyState==4){

    if(xmlhttp.responseText=='OK'){

      alert ('Route Saved');

    }else{

      mapviewer.addEventHandler( 'click', clickHandlerDraw );

      alert ('Error - Not Saved: '+xmlhttp.responseText);

    }

  }

}

 

save.php just writes the "Name" and "ID" values into a database and then echoes "OK".

 

 

This works fine most of the time, but after a while will randomly fail - nothing is returned in the xmlhttp.responseText and the php script does not do the db write. Note that if the db write was failing, I would get mysql error in the xmlhttp.responseText, and so see it in the 'Error - Not Saved' alert.

 

Once the save function has failed once, it will not then work again until I refresh the page.

 

In order to narrow down where the error is, I added the following on another button on my page:

 

function ajaxtest()

{

  xmlhttp=GetXmlHttpObject();

  if (xmlhttp==null)

  {

    alert ("Browser does not support HTTP Request");

    return;

  }

  var url="ajaxtest.htm";

  xmlhttp.onreadystatechange=stateChangedtest;

  xmlhttp.open("GET",url,true);

  xmlhttp.send(null);

}

 

function stateChangedtest()

{

  if (xmlhttp.readyState==4)

  {

  alert(xmlhttp.responseText);

  }

}

 

ajaxtest.htm literally just says "Hello".

 

So, of course, under normal circumstances I can hit the test button and get a "hello" alert, but once the save has failed, hitting the button gives me an empty alert.

 

Fromt his, we can tell that there is no problem specific to the mysql or the save.php script. It's just that the GetXmlHttpObject ajax gubbins has packed in on my main page.

 

It's almost like something has timed out... anybody got any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.