最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript: ajax data's split function error: "Uncaught TypeError: Object has no method 'split

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited Feb 27, 2013 at 6:05 user1517108 asked Feb 27, 2013 at 5:40 user1517108user1517108 2,4156 gold badges34 silver badges44 bronze badges 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 Commented Feb 27, 2013 at 5:48
  • With regards as to your "other" problem, see this: stackoverflow./questions/13593551/… – slebetman Commented Feb 27, 2013 at 6:16
Add a ment  | 

1 Answer 1

Reset to default 6

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论