React native 如何在react native中获取数据?
我需要以下面的格式从pass参数中获取数据,因为当在Postman中进行测试时,只有这种格式给出响应 “json”:{“model”:“DB11 AMR”,“modelyear”:“2019”,“locale”:“AA”} 请您帮助从下面的服务器url获取数据。 下面是我的代码React native 如何在react native中获取数据?,react-native,React Native,我需要以下面的格式从pass参数中获取数据,因为当在Postman中进行测试时,只有这种格式给出响应 “json”:{“model”:“DB11 AMR”,“modelyear”:“2019”,“locale”:“AA”} 请您帮助从下面的服务器url获取数据。 下面是我的代码 var url = 'http://vhapp.azurewebsites.net/myAMLModelSelection' try { let response = fetch
var url = 'http://vhapp.azurewebsites.net/myAMLModelSelection'
try {
let response = fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"json" : { "locale": "AA", "model" : "DB11 AMR", "modelyear" : "2019" }
})
})
.then(res => res.text()) // convert to plain text
.then(text => {
console.log(text)
alert(text)
var res = text.substring(1, text.length-2);
var obj = JSON.parse(res);
alert(obj.cars[0].name)
})
.catch((error) => {
console.error(error);
});
} catch (errors) {
console.log(errors);
}
这是我需要的回答
({“cars”:[{“名称”:“DB11 AMR”,“内容”:{“指南”:”http://cdntbs.astonmartin.com/360ss/OwnersGuides/KY53-19A321-AC.pdf“,”资产“:[{“intAssetId”:“115”,“intVehicleId”:“1”,“strType”:“pdf”,“strName”:“附件手册”,“strDescription”:null,“strLocation”:"http://cdntbs.astonmartin.com/360ss/iPad/myaml/brochures/Accessories 手册英语-706435-PK.pdf,“intVersion:“1”,“intOrder:“1”}]}];您可以使用JS
获取API来获取数据
export async fetchMyData(){
try{
let f = await fetch('https://vhapp.azurewebsites.net/myAMLModelSelection',{method:'GET'})
let data = await f.json();
return data;
}catch(err){
console.log(err)
}
}
并在组件中调用此方法,如:
import {fetchMyData} from './file_name'
fetchMyData().then((response)=>{console.log(response)})
您能解释一下,如何传递参数来获取数据吗?您可以像这样使用fetch POST方法:export async fetchMyData(){try{let f=wait fetch(“”,{method:'POST',data:JSON.stringyfy({name:'John Doe',email:'test@emailer.io'})})let data=wait f.json();返回数据;}catch(err){console.log(err)}