生命不止,继续 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只允许请求当前源(域名、协议、端口)的资源,为了