无法使用JavaScript获取本地API

huangapple go评论115阅读模式
英文:

Can't fetch local API with JavaScript

问题

我用Go编写了一个CRUD API。我在Postman中成功调用了它,但是在JavaScript中使用fetch方法获取数据时,在控制台中出现了以下错误:

  1. todo.js:14 GET https://10.0.154.123:8080/todos net::ERR_SSL_PROTOCOL_ERROR
  2. (anonymous) @ todo.js:14
  3. getData @ todo.js:13
  4. (anonymous) @ todo.js:60
  5. todo.js:14 Uncaught (in promise) TypeError: Failed to fetch
  6. at todo.js:14:9
  7. at new Promise (\<anonymous\>)
  8. at getData (todo.js:13:12)
  9. at todo.js:60:1

这是我的JavaScript中的getData函数:

  1. function getData(urlTail) {
  2. let options = {
  3. method: 'GET',
  4. redirect: 'follow'
  5. }
  6. // console.log(OK)
  7. return new Promise(function(resolve) {
  8. fetch('http://10.0.154.123:8080/todos', options)
  9. .then(response => response.json())
  10. .then(response => {
  11. resolve(response);
  12. })
  13. });
  14. }

以及Go中的代码(使用gin框架):

  1. func main() {
  2. router := gin.Default()
  3. router.GET("/todos", getTodos)
  4. router.GET("/todo/:id", getTodo)
  5. router.POST("/todo", addTodo)
  6. router.DELETE("/todo/:id", removeTodo)
  7. router.PUT("/todo/:id", updateTodo)
  8. router.Run("10.0.154.123:8080")
  9. }

我猜测这是因为它自动将协议更改为https,但我无法修复。希望能得到你的帮助。谢谢。

英文:

I coded a crud API by Go. I called it successfully by Postman, but I fetched it by JavaScript and got this error in console:

  1. todo.js:14 GET https://10.0.154.123:8080/todos net::ERR_SSL_PROTOCOL_ERROR
  2. (anonymous) @ todo.js:14
  3. getData @ todo.js:13
  4. (anonymous) @ todo.js:60
  5. todo.js:14 Uncaught (in promise) TypeError: Failed to fetch
  6. at todo.js:14:9
  7. at new Promise (\<anonymous\>)
  8. at getData (todo.js:13:12)
  9. at todo.js:60:1

This is my getData function in JS:

  1. function getData(urlTail) {
  2. let options = {
  3. method: 'GET',
  4. redirect: 'follow'
  5. }
  6. // console.log(OK)
  7. return new Promise(function(resolve) {
  8. fetch('http://10.0.154.123:8080/todos', options)
  9. .then(response => response.json())
  10. .then(response => {
  11. resolve(response);
  12. })
  13. });
  14. }

and code in Go (gin framework):

  1. func main() {
  2. router := gin.Default()
  3. router.GET("/todos", getTodos)
  4. router.GET("/todo/:id", getTodo)
  5. router.POST("/todo", addTodo)
  6. router.DELETE("/todo/:id", removeTodo)
  7. router.PUT("/todo/:id", updateTodo)
  8. router.Run("10.0.154.123:8080")
  9. }

I guess this because it auto changed the protocol to https but I can't fix. Hope to have your help. Thank you.

答案1

得分: 2

尝试通过不安全的连接(HTTP)从通过安全连接(HTTPS)提供的网页中获取API时,存在安全连接(SSL)的问题。由于同源策略的限制,这是不可能的。要解决此问题,您应该在服务器和客户端之间使用相同的协议(HTTP或HTTPS)。

英文:

There is a problem with the secure connection (SSL) when trying to fetch an API through an insecure connection (HTTP) from a webpage served through a secure connection (HTTPS). It is not possible due to Same-origin Policy. To resolve this issue, you should use same protocol (either HTTP or HTTPS) for both server and client.

huangapple
  • 本文由 发表于 2022年12月22日 22:06:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/74889647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定