Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

javascript - TypeError: Failed to fetch - Only on Cordova application

I have an express server which has an API endpoint of /todo/set which gets a JSON, POST request. This API uses cors.

Additionally, I have a Cordova android application (which built with Svelte) and I'm trying to make a request via fetch to the express server. This is what the request looks like:

fetch(`http://localhost:8080/todo/set`, {
    method: "post",
    body: JSON.stringify({
        'Text': 'Test!'
    }),
    headers: {
        'Content-Type': 'application/json',
    }
})

This isn't working, the server is not getting the request. So I've tried to debug it a little and did this:

fetch(`http://localhost:8080/todo/set`, {
    method: "post",
    body: JSON.stringify({
        'Text': 'Test!'
    }),
    headers: {
        'Content-Type': 'application/json',
    }
}).then(res => alert(res)).catch(err => alert(err))

After running it I've got "TypeError: Failed to fetch" on the alert. So I've tried it once again, This time checked it outside of the Cordova (Only Svelte on a website). This time it's all worked and I got status 200 from the server.

  • It's also tested with Postman and it's working as expected

So I understood that the issue is related to Cordova, But I can't find why it happends. Does someone know how I can solve it?

An update: Tried to add a browser platform to Cordova and tried there. It's working on the browser.

question from:https://stackoverflow.com/questions/65864040/typeerror-failed-to-fetch-only-on-cordova-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've found a solution. If someone comes here after non-stop google search, here it is: Failed to load resource: net::ERR_CLEARTEXT_NOT_PERMITTED


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...