Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,408 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,473 people online right now. Registration is fast and FREE... Join Now!




Serious problem with the Split method

 
Reply to this topicStart new topic

Serious problem with the Split method

cygnusX
15 Oct, 2008 - 06:52 AM
Post #1

D.I.C Head
**

Joined: 19 May, 2007
Posts: 159



Thanked: 2 times
My Contributions
Hey i have these two lines:

CODE

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.
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Serious Problem With The Split Method
15 Oct, 2008 - 07:00 AM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,022



Thanked: 81 times
Dream Kudos: 1175
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

cygnusX
RE: Serious Problem With The Split Method
15 Oct, 2008 - 10:26 AM
Post #3

D.I.C Head
**

Joined: 19 May, 2007
Posts: 159



Thanked: 2 times
My Contributions
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:

CODE
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);
}

User is offlineProfile CardPM
+Quote Post

xerxes333
RE: Serious Problem With The Split Method
15 Oct, 2008 - 11:25 AM
Post #4

D.I.C Head
Group Icon

Joined: 5 Jul, 2007
Posts: 181



Thanked: 10 times
Dream Kudos: 25
My Contributions
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.

User is online!Profile CardPM
+Quote Post

cygnusX
RE: Serious Problem With The Split Method
24 Oct, 2008 - 06:14 AM
Post #5

D.I.C Head
**

Joined: 19 May, 2007
Posts: 159



Thanked: 2 times
My Contributions
QUOTE(xerxes333 @ 15 Oct, 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.

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:

CODE
//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:

CODE
//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?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:15PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month