官术网_书友最值得收藏!

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"})  
主站蜘蛛池模板: 西城区| 威海市| 平顶山市| 大同市| 阳原县| 邢台市| 白河县| 汤阴县| 关岭| 洪湖市| 寿阳县| 平凉市| 赣榆县| 新巴尔虎左旗| 乌苏市| 郎溪县| 龙里县| 沅陵县| 双城市| 上思县| 昂仁县| 镇原县| 邯郸市| 绍兴市| 莱阳市| 北流市| 信阳市| 驻马店市| 陆丰市| 雅安市| 夏河县| 开江县| 海宁市| 威海市| 武平县| 樟树市| 宁化县| 潞城市| 富蕴县| 安顺市| 平果县|