1:100 Degree Introduction:
JSONP (JSON with Padding) is a "usage pattern" of JSON that can be used to solve the problem of cross-domain data access for mainstream browsers. because of the same-origin policy, Web pages that are generally located in server1.example.com cannot communicate with servers that are not server1.example.com, while HTML <script> elements are an exception. Using this open strategy of <script>, the Web page can get JSON data that is dynamically generated from other sources, and this usage pattern is called JSONP. The data captured with JSONP is not JSON, but arbitrary JavaScript, executed with a JavaScript interpreter instead of parsing with a JSON parser
Two: Front-end use:
<script type= "Text/javascript" >
function Test () {
$.ajax ({
type: "POST",
DataType: ' Jsonp ',
JSONP: ' Callback ',
Data: $ (' #register_formId '). Serialize (),
URL: ' Http://192.168.191.1:8080/H5Web/menushowmanage/menushowmanage/jsonpTest?Callback=? ',
Success:function (data) {
if (data.result_code== "200") {
Alert ("Your operation executed successfully!") ");
}else{
Alert ("Operation failed")
}
},error:function (XMLHttpRequest, Textstatus, Errorthrown) {
alert ("404");
alert (xmlhttprequest.status);
alert (xmlhttprequest.readystate);
alert (textstatus);
alert (Xmlhttprequest.responsetext);
}
});
}
</script>
Three: Back end example
Private String callback;//front-end JSONP dynamically generated function name
/**
* Get and Set methods
*/
Public String Getcallback () {
return callback;
}
public void Setcallback (String callback) {
This.callback = callback;
}
/**
* How to use JSONP
*/
Public void Jsonptest () {
System.out.println ("---URL:" +url+ "--callback:" +callback);
Gson Gson=constants.gson;
baseres bs=new baseres ();
Bs.setresult_code (SuccessCode.CLIENT_SUCCESS.getResult ());
This.print (Callback+ "(" +gson.tojson (BS) + ")");//JSONP return format, callback represents a set of random methods that the front-end JSONP commits to the background, but the callback name is aligned with the front end
}
Returns the format of the front page JSONP:
jquery18007564855464734137_1431501866366 ({"Result_code": "200"})
Where jquery18007564855464734137_1431501866366 is a callback value that passes through the front-end randomly generated pass through the background
The use of STURTS2+JSONP in the----of Tian Yi