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

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"})  
主站蜘蛛池模板: 台中市| 平阴县| 锡林浩特市| 城步| 车险| 沾益县| 庆安县| 三亚市| 萨嘎县| 大埔县| 谢通门县| 灵璧县| 长沙县| 普兰店市| 射阳县| 济南市| 铁岭市| 家居| 龙南县| 襄垣县| 高清| 通州市| 鹤壁市| 柳林县| 防城港市| 兴业县| 格尔木市| 杭州市| 通化县| 潮安县| 墨竹工卡县| 义马市| 鄯善县| 工布江达县| 洪洞县| 芮城县| 平远县| 金华市| 邳州市| 车致| 深泽县|