indu-prakash / jquery-csv

Automatically exported from code.google.com/p/jquery-csv
MIT License
0 stars 0 forks source link

SCRIPT438: Object doesn't support property or method 'replace': jquery.csv-0.64.js, line 220 character 7 #12

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
Loading CSV file via the following 

var csvData = $.ajax({ 
    type: "GET", 
    url: "my.csv", 
    dataType: "text", 
    error: function ( xhr, errorType, exception ) {
        var errorMessage = exception || xhr.statusText;
        alert( "There was an error loading your data file: " + errorMessage );  
    },
    success: function (result) { 
    alert(result); <= shows that CSV data loadad
    //alert($.type(csvData)); <= returns 'object'
    //alert("done!"+ csvData.getAllResponseHeaders()) 
    myCSVArray = $.csv.toArray(csvData); <= breaks.
    //alert( $.toJSON(myCSVArray) ); 
    } 
}); 

What is the expected output? What do you see instead?
IE Debugger captures the following error
SCRIPT438: Object doesn't support property or method 'replace' 
jquery.csv-0.64.js, line 220 character 7

Original issue reported on code.google.com by gcho...@gmail.com on 10 Oct 2012 at 8:14

GoogleCodeExporter commented 8 years ago
It's failing because you're calling it on the wrong variable.

csvData, as defined in your code contains an object representing the jQuery 
AJAX request not the data containing the contents of the response. 
$.csv.toArray() works on strings, hence the requirement for the replace() 
method (which is a method of the string prototype).

What you really want is the 'result' variable, which contains the csv data 
string. You have demonstrated as much with the following statement.

    alert(result); <= shows that CSV data loadad.

Here's what your code should look like:

    var csvData = $.ajax({ 
      type: "GET", 
      url: "my.csv",
      dataType: "text", 
      error: function ( xhr, errorType, exception ) {
        var errorMessage = exception || xhr.statusText;
        alert( "There was an error loading your data file: " + errorMessage );  
      },
      success: function (result) { 
        alert(result); <= shows that CSV data loadad
        //alert($.type(result)); <= returns 'string'
        //alert("done!"+ csvData.getAllResponseHeaders()) 
        var myCSVArray = $.csv.toArrays(csvData); <= breaks.
        //alert( $.toJSON(myCSVArray) ); 
      }
});

Note: You also want to call toArrays() for multi-line CSV data since toArray() 
is only designed to handle a single entry.

The parser isn't broken, you were just calling the wrong variable. I don't have 
any good examples demonstrating jquery/AJAX usage but I plan on adding them in 
the near future.

I'm going to mark this as fixed but leave it open for a bit so you can report 
any other issues if the fix doesn't work.

Original comment by evanpla...@gmail.com on 10 Oct 2012 at 9:27

GoogleCodeExporter commented 8 years ago

Original comment by evanpla...@gmail.com on 10 Oct 2012 at 9:28

GoogleCodeExporter commented 8 years ago

Original comment by evanpla...@gmail.com on 11 Oct 2012 at 4:08