1

In my code I get the data from jQuery ajax and assigning it to a array variable. But when I call split function on this array variable I get the error (as seen in google chrome console) :

Uncaught TypeError: Object  has no method 'split' 

The relevant code is:

$(function () {
    var originalData=[]; // Also tried var originalData=new Array(); and var originalData;
    $.ajax({                                      
      url: 'data.php',                    
      data: "",                        
      dataType: 'text',               
      success: function(data)          
      {
        alert(data); // THIS WORKS
      }
    });

    alert("a");  // THIS WORKS
    var dataArray =originalData.split("#");
    alert("abc "+ dataArray); //THIS DOESNT WORK
});

Not able to crack this at all. Don't know if I am making a mistake in assuming object type of ajax data or in assigning orginalData to data.

Also the php file's output is echo $array; where $array is declared as $array=""; and thereafter I keep appending strings to $array.

Also the data content is: #195:93.0500:94.8500:93.0500:93.6500#196:94.0000:94.4500:92.0000:92.2500#197:91.0000:92.1000:87.6500:87.6500#198:88.0000:89.0000:86.0000:87.9000#199:89.0000:92.3000:88.5000:92.2000#200:93.1000:94.1000:90.7500:91.4000

Thanks

EDIT

After Aashray's answer, by substituting var originalData=[]; to var originalData=""; I am not getting the error. However the functionality of split is not working as it shows a blank array after split.

EDIT

The issue of blank array does not seem related to original question. I think the problem is that $.ajax function is being called after the split function. Atleast when i put in more alerts I found that the sequence starts with alerts around split function, and the alert inside $.ajax happens at end. So the blank array after split is blank because most probably its input data was not populated by $.ajax at the time split was called. But then thats another problem unrelated to the question.

2
  • alert(data); // THIS WORKS can you post the output of that alert. and also where are you assigning data to the variable originalData.
    – Sandeep
    Feb 27, 2013 at 5:48
  • With regards as to your "other" problem, see this: stackoverflow.com/questions/13593551/…
    – slebetman
    Feb 27, 2013 at 6:16

1 Answer 1

6

originalData is an array. split() function can only be used on strings. Use an index to refer the location, like originalData[0].

6
  • But when I declare original data as "var originalData;" and then assign it to data, then it should work. But it doesnt. Feb 27, 2013 at 5:48
  • @Aashray: It worked somewhat after i assigned var originalData="";. It alerted "abc" but did not alert the rest of the dataArray. So atleast the code recognizes the "split" function, though it isnt working properly. Thanks. Feb 27, 2013 at 5:55
  • again dataArray is an array. I don't think there is a toString(); function in js.
    – Aashray
    Feb 27, 2013 at 5:57
  • @Aashray: Marked your answer as correct as it answers my issue. I think the problem is coming that $.ajax function is being called after the split function. Atleast when i put in more alerts I found that the sequence starts with alerts around split function, and the alert inside $.ajax happens at end. So the blan array after split is blank because most probably its input data was not populated by $.ajax at the time split was called. But then thats another problem unrelated to the question. Feb 27, 2013 at 6:03
  • Your alert(data); displays the values obtained right? where are you assigning the value of data to originalData. Check this part. If you are getting the values form ajax, then they need to be assigned to a local variable.
    – Aashray
    Feb 27, 2013 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.