2017-09-07 98 views
-3

我是JavaSript和NativeScript的新手。我可以從API調用中獲取JSON字符串。我試圖解析通過JSON.parse它,但不幸的是我沒有得到任何東西。任何人都可以幫助嗎?如何從Javascript中檢索JSON數據

exports.signIn = async function() { 

await fetchModule.fetch(apiurl,{ 
    method:"POST", 
    body:JSON.stringify({"username":username ,"password":password}), 
    headers: { 
     "Content-Type": "application/json" 
     } 
}).then(function(response){ 
    var result = JSON.parse(response) 
    alert(result.username) 
}) 

}

+0

太少的信息。請發佈數據示例 –

+2

您嘗試過什麼?你卡在哪裏?我們不想只向您提供解決方案;我們希望你獲得諒解。但是,因爲我們不知道你的根本問題是什麼,所以我們不能開始提供幫助。有關提問的提示,請參見[如何提問](https://stackoverflow.com/help/how-to-ask)。 – Cerbrus

+0

如果你傳遞了一個json對象,爲什麼你需要解析它?你是否在http請求中包含'.map(res => res.json())'? – mast3rd3mon

回答

2

使用本

exports.signIn = async function() { 

await fetchModule.fetch(apiurl,{ 
    method:"POST", 
    body:JSON.stringify({"username":username ,"password":password}), 
    headers: { 
     "Content-Type": "application/json" 
     } 
}).then(function(response){ 
    alert(response.username) 
}) 
+0

非常感謝。這是工作!! – Shreya