2017-09-14 2 views
-1

Code:Wie bekomme ich AntwortText von JavaScript Variable?

<script> 
    $(document).ready(function(){ 

     var filepath = 'csv/data.csv';      
     var data_string = $.get(filepath); 
     console.log(data_string); 
    }); 
</script> 

Als ich console.log(data_string) verwende ich auf der Konsole die folgende Ausgabe bekam.

Als ich das gelesen habe, fand ich, dass "responseText" die Werte hat, die ich will. Also muss ich nur "responseText" auf eine andere Variable bekommen.

Ich versuchte var data = data_string.responseText. Aber es hat nicht funktioniert.

enter image description here

+0

var response = data_string [response] –

+0

@HarshPatel - Das wird nicht funktionieren. – Quentin

+0

Haben Sie über [Dokumentation gelesen] (https://api.jquery.com/jquery.get/) nachgedacht? – Quentin

Antwort

1

Sie sollen eine callback Funktion $.get befestigen. Von Ihrer Konsole aus sehe ich, dass es sich um eine jqXHR Object handelt. $.get Methode hat eine success Callback-Funktion, die ausgeführt wird, wenn die request erfolgreich ist.

Attaching eine Callback-Funktion enthält automatisch json geparst.

var filepath = 'csv/data.csv';  
$.get(filepath , function(response) { 
    console.log(response); 
}); 
0

Sie benötigen Callback-Funktion zur Verfügung zu stellen Antwort vom Server zu erhalten wie unten:

var filepath = 'csv/data.csv';  
$.get(filepath , function(data) { 
console.log(data); 
}); 
Verwandte Themen