React Native Expo App holt keinen Endpunkt ab
Posted: 27 Feb 2025, 02:30
Ich habe eine Webanwendung, die einen REST -API -Endpunkt in einer Domäne aufdeckt, und dann habe ich eine reagierte native Anwendung, die diesen Endpunkt verbraucht, der sie abholt. Während die Expo -Go -Anwendung funktioniert (selbst wenn die API eine externe Domäne und nicht localhost ist), zeigt der Android Studio -Gerätesimulator keine Daten an. /> So sieht mein API-Endpunkt aus: < /p>
Die externe Domäne hat die externe Domäne unter https: //mydomain.ext . Was mache ich falsch? Wie repariere ich es?
Code: Select all
export async function GET(req: NextRequest) {
// some other code
return NextResponse.json({ data }, {
headers: {
'Access-Control-Allow-Origin': '*',
}
})
}
< /code>
Und dies ist der native React-Code: < /p>
interface Data {
id: number
name: string
}
export default function SearchScreen() {
const [searchQuery, setSearchQuery] = useState("")
const [results, setResults] = useState([])
const handleSearch = async () => {
const apiUrl = `${process.env?.EXPO_PUBLIC_BASE_URL}/api/search`
try {
const response = await fetch(`${apiUrl}?k=${searchQuery.toLowerCase()}`)
const data = await response.json()
setResults(data)
} catch (err) {
// set the error
}
}
return (
{/* display the data */}
)
}