- 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"})
- Rust實(shí)戰(zhàn)
- 騰訊iOS測(cè)試實(shí)踐
- HTML5 移動(dòng)Web開(kāi)發(fā)從入門(mén)到精通(微課精編版)
- 算法基礎(chǔ):打開(kāi)程序設(shè)計(jì)之門(mén)
- 碼上行動(dòng):零基礎(chǔ)學(xué)會(huì)Python編程(ChatGPT版)
- Visual Basic程序設(shè)計(jì)教程
- Essential Angular
- 深入淺出RxJS
- Learning Concurrent Programming in Scala
- Python極簡(jiǎn)講義:一本書(shū)入門(mén)數(shù)據(jù)分析與機(jī)器學(xué)習(xí)
- Java Web開(kāi)發(fā)詳解
- Learning YARN
- Python 快速入門(mén)(第3版)
- 自己動(dòng)手構(gòu)建編程語(yǔ)言:如何設(shè)計(jì)編譯器、解釋器和DSL
- React.js實(shí)戰(zhàn)