2017-09-14 80 views
-1

代碼:如何從JavaScript變量獲取responseText?

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

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

當我使用console.log(data_string)我得到了控制檯上顯示以下輸出。

當我讀到,我發現「responseText」具有我想要的值。所以我只需要獲得「responseText」給另一個變量。我試過var data = data_string.responseText。但它沒有奏效。

enter image description here

+0

var responseText = data_string [responseText] –

+0

@HarshPatel - 這是行不通的。 – Quentin

+0

你有沒有考慮[閱讀文檔](https://api.jquery.com/jquery.get/)哪個例子? – Quentin

回答

1

你應該附加一個callback功能$.get。從你的控制檯我看到這是一個jqXHR Object$.get方法具有success回調函數,該函數在request成功時執行。

Attaching回調函數自動包含json解析。

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

您需要提供的回調函數從服務器獲取響應象下面這樣:

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