The general handler uses context. request ["callback"]; obtain the content of the page callback function, and then process the Request, after obtaining the information to be sent to the client (this process can be completed by passing more parameters to guide the general processing program), Response. write () is returned to the page, and its content is the defined callback function, and the data is responded to the page "call (the data obtained after processing)" in the form of function parameters )". After the response is complete, we can re-modify the current page structure, which should be:
<Htmlxmlns = "http://www.w3.org/1999/xhtml"> <? Xml: namespace prefix = o ns = "urn: schemas-microsoft-com: office"/>
<Head>
<Title> </title>
Call (the data obtained after processing );
<Scripttype = "text/javascript">
Function call (data ){
Alert (data );
}
</Script>
</Head>
<Body>
</Body>
</Html>
Now, the defined callback function will generate a warning by alert, showing that the page has obtained data, so that a jsonp is complete.
In addition, we know that the lightweight structure of JSON is very suitable for data transmission. If I add a parameter during the request, for example, dataType = 'json', after the handler obtains this parameter, I knew that the client had to convert the processed data into json format and then transmit it. In this way, we can give the data json (the server processing program adds a sentence ):
System. Web. Script. Serialization. JavaScriptSerializer jz = new System. Web. Script. Serialization. JavaScriptSerializer ();
String pJson = jz. Serialize (callData );
Then, the client can directly parsePSON () and then display the data, which is very convenient.
JQuerySlave1.2TheJSONPSolution and encapsulation:
That is, the method: $. getJSON (url, function (data ));
The principle is the same. In this method, the url must contain a parameter indicating the callback function, but the name of the function does not need to be explicitly written. JQuery will generate it by itself, so the url write structure is: myurl? Callback = ?. This is not the final request address, callback =? The question mark in JQuery will automatically generate a function name, which corresponds to the callback function name, but the callback must still be in the context of the server. request ["callback"]; otherwise, how does the server know which parameter represents the function name?
The callback function is the second parameter of the method, and data is the return value. In one sentence, we have implemented JSONP, which is very convenient. However, in any case, this method still has the following Disadvantages:
1: When a cross-origin behavior error occurs, the page cannot be captured, and the page will not do anything.
2: The server and client must cooperate with each other, for example, passing the callback function parameter key, and so on. It is still not flexible.