The use of STURTS2+JSONP in the----of Tian Yi

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.