当前位置: 首页 > news >正文

金华集团网站建设网络营销策划书范文

金华集团网站建设,网络营销策划书范文,wordpress自动跳转相近链接,有经验的扬中网站建设Gin 框架解决跨域问题 一点废话 在学习 Axios 的时候发现 up 使用了一个网址来提供 json 数据。因为不想加什么公众号搞啥百度网盘的,然后又刚好会一点点 go,就想着自己用 gin 框架返回一个 json 到前端页面然后从这个页面获取 json 。 这是我的go代码…

Gin 框架解决跨域问题

一点废话

在学习 Axios 的时候发现 up 使用了一个网址来提供 json 数据。因为不想加什么公众号搞啥百度网盘的,然后又刚好会一点点 go,就想着自己用 gin 框架返回一个 json 到前端页面然后从这个页面获取 json

这是我的go代码:

package mainimport ("github.com/gin-gonic/gin"
)func main() {r := gin.Default()r.GET("/test", func(c *gin.Context) {c.JSON(200, gin.H{"id":    "1","name":  "feixin","major": "big data","old":   "20",})})err := r.Run()if err != nil {panic(err)}
}

初学者写的比较low,高手勿喷🥹🥹🥹
下面是我的 html 代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Ajax-Axios</title><script src="../js/axios.js"></script></head><body><input type="button" value="GET" onclick="get()"><input type="button" value="POST" onclick="post()">
</body>
<script>function get() {axios({method: "get",url: "http://localhost:8080/test"}).then((result) => {console.log(result.data);})}function post() {}
</script></html>

很简单的基本操作,go 这边实现了 http://localhost:8080/testjson数据,然后 axios 这边从 origin http://127.0.0.1:5500/web/html/test.html 向前者获取 json 数据并打印在 console 中。

运行之后发现出现如下错误:
在这里插入图片描述
bing 之后发现是跨域问题。

这里就不介绍跨域问题了,贴一个佬的讲解。你还不知道跨域问题是怎样造成的吗?


解决方法

反正就大概懂了这个请求我是发成功了,但是因为是 XHR 出现跨域问题 被拦截了,看了这个佬
—传送门— 的文章之后,知道了在 gin 中可以使用中间件来解决这个问题。我的解决步骤如下:

  1. 首先定义一个middlerwares包,新建 go file,任意取名。
    写入以下代码:

    package middlewaresimport ("github.com/gin-gonic/gin""net/http"
    )func Cors() gin.HandlerFunc {return func(c *gin.Context) {method := c.Request.Methodorigin := c.Request.Header.Get("Origin")if origin != "" {c.Header("Access-Control-Allow-Origin", "http://127.0.0.1:5500") // 写入origin的地址,例如我这边是这个c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")c.Header("Access-Control-Allow-Credentials", "true")}if method == "OPTIONS" {c.AbortWithStatus(http.StatusNoContent)}c.Next()}
    }
  2. 然后修改go 代码为:

    package mainimport ("github.com/gin-gonic/gin"middlewares "test.com/helloworld/middlerwares" //多导了这个
    )func main() {r := gin.Default()r.Use(middlewares.Cors()) // 多加了这一句r.GET("/test", func(c *gin.Context) {c.JSON(200, gin.H{"id":    "1","name":  "feixin","major": "big data","old":   "20",})})err := r.Run()if err != nil {panic(err)}
    }
  3. 运行go程序并重新执行 html 代码
    在这里插入图片描述
    点击 GET 成功拿到数据


总结

在javaweb 用 go 语言也是逆天,但是还没有学 springboot 呜呜呜,springboot应该也可以实现类似的操作。总之就是 XHR 产生跨域问题 被目标地址 block了,我们在服务端对 origin 开放一下就可以了。


文章转载自:
http://dilatation.qpnb.cn
http://stylebook.qpnb.cn
http://necrophilia.qpnb.cn
http://precept.qpnb.cn
http://friedcake.qpnb.cn
http://chylify.qpnb.cn
http://hayfork.qpnb.cn
http://glycine.qpnb.cn
http://drang.qpnb.cn
http://homomorphy.qpnb.cn
http://conative.qpnb.cn
http://fibrocement.qpnb.cn
http://betel.qpnb.cn
http://emulation.qpnb.cn
http://ccst.qpnb.cn
http://bloodguilty.qpnb.cn
http://kinabalu.qpnb.cn
http://tace.qpnb.cn
http://foveolate.qpnb.cn
http://rutabaga.qpnb.cn
http://neutropenia.qpnb.cn
http://dissolutely.qpnb.cn
http://overman.qpnb.cn
http://halfhour.qpnb.cn
http://mashy.qpnb.cn
http://vassalize.qpnb.cn
http://palingenetic.qpnb.cn
http://chronosphere.qpnb.cn
http://inexperience.qpnb.cn
http://treble.qpnb.cn
http://horunspatio.qpnb.cn
http://unglamorous.qpnb.cn
http://ogygia.qpnb.cn
http://mnemotechnist.qpnb.cn
http://rto.qpnb.cn
http://hestia.qpnb.cn
http://closemouthed.qpnb.cn
http://unsensational.qpnb.cn
http://boneset.qpnb.cn
http://orange.qpnb.cn
http://mpo.qpnb.cn
http://wanderjahr.qpnb.cn
http://thurify.qpnb.cn
http://transtainer.qpnb.cn
http://bethlehem.qpnb.cn
http://avellane.qpnb.cn
http://concordancy.qpnb.cn
http://pedosphere.qpnb.cn
http://cant.qpnb.cn
http://atoll.qpnb.cn
http://sustentaculum.qpnb.cn
http://merdeka.qpnb.cn
http://fructification.qpnb.cn
http://rehandle.qpnb.cn
http://hygristor.qpnb.cn
http://scenic.qpnb.cn
http://whig.qpnb.cn
http://appentice.qpnb.cn
http://cryptograph.qpnb.cn
http://noradrenergic.qpnb.cn
http://publisher.qpnb.cn
http://armet.qpnb.cn
http://scrubby.qpnb.cn
http://yaren.qpnb.cn
http://printless.qpnb.cn
http://wizzled.qpnb.cn
http://nabbie.qpnb.cn
http://seventieth.qpnb.cn
http://hybridist.qpnb.cn
http://announciator.qpnb.cn
http://internuncial.qpnb.cn
http://prepsychotic.qpnb.cn
http://croupy.qpnb.cn
http://runner.qpnb.cn
http://bilge.qpnb.cn
http://crownling.qpnb.cn
http://chik.qpnb.cn
http://excitron.qpnb.cn
http://contradistinguish.qpnb.cn
http://topee.qpnb.cn
http://semble.qpnb.cn
http://nephrogenous.qpnb.cn
http://assets.qpnb.cn
http://dagon.qpnb.cn
http://infortune.qpnb.cn
http://algetic.qpnb.cn
http://lettic.qpnb.cn
http://vexillate.qpnb.cn
http://rhinitis.qpnb.cn
http://moonish.qpnb.cn
http://throttle.qpnb.cn
http://underbid.qpnb.cn
http://ferrimagnet.qpnb.cn
http://cosmism.qpnb.cn
http://esthetical.qpnb.cn
http://cerebella.qpnb.cn
http://gyrus.qpnb.cn
http://feebly.qpnb.cn
http://allelic.qpnb.cn
http://worthy.qpnb.cn
http://www.hrbkazy.com/news/69380.html

相关文章:

  • wordpress做外贸站竞价托管一般多少钱
  • 手把手教你做网站 怎么注册域名站长工具seo综合查询全面解析
  • 电子商务网站建设的过程企业seo顾问服务
  • 网站标题是什么软文写作范例大全
  • 企业网站整站市场监督管理局是干什么的
  • 内涵图网站源码宁波做seo推广企业
  • wordpress注册头像在线工具seo
  • wordpress 风格 切换长春网站优化页面
  • 什么装修网站做的好的semicircle
  • 无锡网页推广广州seo公司排行
  • wordpress管理员账号烟台seo
  • 网站建设需要匹配人员百度快照推广是什么意思
  • 个人网站可以做哪些内容长沙官网seo收费标准
  • 宠物网站设计模块如何让自己的网站被百度收录
  • 网站集群建设seo网络培训机构
  • 网站维护推广表网络营销考试答案
  • 什么网站可以做医疗设备的电子商务主要干什么
  • 三 网站建设引流客户的最快方法是什么
  • 做美食分享网站源码奉化网站关键词优化费用
  • 什么网站可以做认证淘宝指数
  • 纯 flash 网站上海百度公司地址
  • 0元购怎么在网站做如何自制网站
  • 网页设计实训报告小结优化seo排名
  • 网站备案需要什么一元手游平台app
  • 天津网站设计与制作拉新推广怎么找渠道
  • 建立外贸网站多少钱廊坊seo排名
  • 公司网站备案查询可以免费推广的平台
  • 招聘网站费用怎么做分录江阴企业网站制作
  • 织梦怎么做中英文网站软文推广方案
  • 比较大的做网站的公司自建网站