- Building Microservices with Go
- Nic Jackson
- 426字
- 2021-07-15 17:28:13
JSONP
JSONP is pretty much a hack, and it is implemented by most browsers that do not implement the later CORS standard. It is restricted to GET requests only and works by getting round the issue that while XMLHTTPRequest is blocked from making requests to third-party servers, there are no restrictions on HTML script elements.
A JSONP request inserts a <script src="..."> element into the browsers DOM with the API's URI as the src target. This component returns a function call with the JSON data as a parameter, and when this loads, the function executes passing the data to the callback.
JavaScript callback is defined in the code:
function success(data) {
alert(data.message);
}
This is the response from the API call:
success({"message":"Hello World"})
To denote a request for data to be returned as JSONP, generally the callback=functionName parameter is added to the URI, in our example this would be /helloworld?callback=success. Implementing this is particularly straightforward let's take a look at our simple Go helloworld example and see how we can modify this to implement JSONP.
One thing to note is the Content-Type header that we are returning. We are no longer returning application/json as we are not returning JSON we are actually returning JavaScript so we must set the Content-Type header accordingly:
Content-Type: application/javascript
Example chapter2/jsonp/jsonp.go:
Let's take a quick look at an example of how we can send JSONP with Go, our response object is going to be exactly the same as the ones in Chapter 1, Introduction to Microservices:
18 type helloWorldResponse struct {
19 Message string `json:"message"`
20 }
The difference is all in the handler, if we look at line 30 we are checking to see if there is a callback parameter in the query string. This would be provided by the client and indicates the function they expect to be called when the response is returned:
23 func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
24 response := helloWorldResponse{Message: "HelloWorld"}
25 data, err := json.Marshal(response)
26 if err != nil {
27 panic("Ooops")
28 }
29
30 callback := r.URL.Query().Get("callback")
31 if callback != "" {
32 r.Headers().Add("Content-Type", "application/javascript")
33 fmt.Fprintf(w, "%s(%s)", callback, string(data))
34 } else {
35 fmt.Fprint(w, string(data))
36 }
37 }
To return our response in JSONP format all we need to do is wrap the standard response to a JavaScript function call. In line 33, we are taking the callback function name that was passed by the client and encapsulating the response we would normally send. The resultant output would look something like this:
Request:
GET /helloworld?callback=hello
Response:
hello({"message":"Hello World"})
- Learning Cython Programming
- 小創(chuàng)客玩轉(zhuǎn)圖形化編程
- Python神經(jīng)網(wǎng)絡項目實戰(zhàn)
- jQuery從入門到精通 (軟件開發(fā)視頻大講堂)
- 零基礎學MQL:基于EA的自動化交易編程
- 機械工程師Python編程:入門、實戰(zhàn)與進階
- iOS應用逆向工程(第2版)
- 自制編程語言
- Mastering JavaScript High Performance
- MySQL從入門到精通(軟件開發(fā)視頻大講堂)
- Android項目實戰(zhàn):手機安全衛(wèi)士開發(fā)案例解析
- 寫給青少年的人工智能(Python版·微課視頻版)
- Software-Defined Networking with OpenFlow(Second Edition)
- Spring Boot 3:入門與應用實戰(zhàn)
- 區(qū)塊鏈:技術與場景