框架 go_Go实战--使用echo框架中JSONP

本文介绍了JSONP的概念及其在JavaScript中的应用,探讨了为何需要JSONP来规避同源策略限制。接着,文章详细阐述了在Golang中如何利用Echo框架实现JSONP,包括获取前端回调函数名、编码JSON数据、设置HTTP响应的Content-Type以及返回JSONP格式的响应。同时,提供了JS中使用JSONP的示例和在Echo框架中使用JSONP的代码参考。
摘要由CSDN通过智能技术生成

生命不止,继续 go go go !!!

继续,echo web框架,今天就聊一聊JSONP。

JSONP

1、什么是JSONP?

JSONP (JSON with padding) is used to request data from a server residing in a different domain than the client. It was proposed by Bob Ippolito in 2005.

JSONP enables sharing of data bypassing same-origin policy. The policy disallows running JavaScript to read media DOM elements or XHR data fetched from outside the page’s origin. The aggregation of the site’s scheme, port number and host name identifies as its origin. Due to inherent insecurities, JSONP is being replaced by CORS.

关于golang中cors可以参考:Go实战–golang中使用echo框架中的cors(labstack/echo、rs/cors)

要了解JSONP,不得不提一下JSON,那么什么是JSON ?

JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.

关于golang中使用json可以参考:
Go语言学习之encoding/json包(The way to go)
Go实战–net/http中JSON的使用(The way to go)

JSONP(JSON with Padding)是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式)。

JSONP有什么用
由于同源策略的限制,XmlHttpRequest只允许请求当前源(域名、协议、端口)的资源,为了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值