4 Replies - 11686 Views - Last Post: 24 October 2008 - 07:14 AM

#1 cygnusX   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Serious problem with the Split method

Post icon  Posted 15 October 2008 - 07:52 AM

Hey i have these two lines:

var product= xmlHttp.responseText.split("#");
var prices = product[1].split("+");



Everything is ok in Firefox and Opera but nothing happens in IE,Safari and Google Chrome.I mean the execution of the code just stops at the second line.Here's the error that i'm receiving in Chrome: Uncaught TypeError: Cannot call method 'split' of undefined...I don't understand why it doesn't work in those three browsers but only in FF and Opera.

Is This A Good Question/Topic? 0
  • +

Replies To: Serious problem with the Split method

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1649
  • View blog
  • Posts: 8,520
  • Joined: 07-September 06

Re: Serious problem with the Split method

Posted 15 October 2008 - 08:00 AM

Have you made sure that you are only splitting after the whole response text has been recieved?

The server responds in portions, not as a whole, that means you must make sure that you have all the response otherwise you could get errors.

In general it should work the same for all 3 browsers (considering it is built into Javascript and they all have javascript capabilities).

More code could be helpful too.
Was This Post Helpful? 0
  • +
  • -

#3 cygnusX   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Re: Serious problem with the Split method

Posted 15 October 2008 - 11:26 AM

What i've found is that the xmlhttp.responseText property is empty in IE,Safari and Chrome.That's why i have problem with the split method.And i don't know how to fix that.Here is how i create the xmlhttp object and the function for sending request to the server and handling retrieved information from it:

 var xmlHttp = http();
  
  function http()
  {
	 try
	 {
		xmlHttp=new XMLHttpRequest();
	 }
	 catch (e)
	 {
		try
		{
		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
		  try
		  {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		  catch (e)
		  {
			alert("Your browser does not AJAX");
			return false;
		  }
	   }
	 }
   return xmlHttp;
 }

 function retrieveProduct(productName)
   {
	 var parameters;

	 xmlHttp.onreadystatechange = function() {
	   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

		  var product= xmlHttp.responseText.split("#");
		  var prices = product[1].split("+");
		  //some code here...
		}
   
	  xmlHttp.open("POST","products.php",true);
	  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8;");
	  xmlHttp.send(parameters);
}


Was This Post Helpful? 0
  • +
  • -

#4 xerxes333   User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 504
  • Joined: 05-July 07

Re: Serious problem with the Split method

Posted 15 October 2008 - 12:25 PM

I didn't look into the code too much but why not just put some error checking around var product= xmlHttp.responseText.split("#"); so the browser wont error out.
Was This Post Helpful? 0
  • +
  • -

#5 cygnusX   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Re: Serious problem with the Split method

Posted 24 October 2008 - 07:14 AM

View Postxerxes333, on 15 Oct, 2008 - 12:25 PM, said:

I didn't look into the code too much but why not just put some error checking around var product= xmlHttp.responseText.split("#"); so the browser wont error out.

Uuh i fixed the split error,but the problem is that responseText property is empty.I'm not sure whether this causes the problem but i should ask,so...

What's the difference between this:

//the functions for handling the incoming from the server data are separate functions
function retrieveProduct(productName)
   {
	 //some code here
	 //parameters = blahblah;

	  xmlHttp.open("POST","products.php",true);
	  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8;");
	  xmlHttp.send(parameters);
}

function doSomething()
{
	  //some code here
	  //parameters = blahblah;
  
	  xmlHttp.open("POST","products.php",true);
	  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8;");
	  xmlHttp.send(parameters);
}


and this:

//all ''functions" for handling the incoming from the server data are inside one main function
function handleIncomingData()
{
   if(something)
  {
	parameters = "var1=value";
	xmlHttp.onreadystatechange = function() {
	  //do something
	}
  }

 else
  {
	parameters = "var1=value";
	xmlHttp.onreadystatechange = function() {
	  //do something else
	}
  }

	  xmlHttp.open("POST","products.php",true);
	  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8;");
	  xmlHttp.send(parameters);
}


I mean is there some significant difference between those two methods other than just different organizing of the source code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1