Use flyjsonp and jquery to implement cross-origin asynchronous requests.

Source: Internet
Author: User

Go directlyCodeAll the introduction is in the code comment.

First, use flyjsonp to implement cross-origin asynchronous requests.

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <SCRIPT type = "text/JavaScript" src = "JS/flyjsonp. min. JS "> </SCRIPT> <SCRIPT type =" text/JavaScript "> function getjfbcustomstate () {flyjsonp. init ({Debug: true}); // initialize the instance of flyjsonp. The debug parameter can be set to true or falseflyjsonp. post ({URL: 'http: // 123. 125. **. * **: 8088/ecpaycus/web/getcustomizebyphoneno ', parameters: {phoneno: '000000'}, success: function (data) {var customstate = data. customstate; alert ('server return result: '+ customstate);}/* success: function (data) {var customstate = data. customstate; alert ('server return result: '+ customstate) ;}, error: function (errormsg) {alert ('22'); console. log (errormsg);} */});} </SCRIPT> <span style = "color: Blue; text-Decoration: underline; cursor: pointer; "onclick =" getjfbcustomstate (); "> click here to complete the customization </span> <% -- ============================ ========================================================== ==================================== flyjsonp homepage: flyjsonp does not rely on any JavaScript framework. You only need to set some parameters to use it to implement cross-origin post and GET requests. In this example, ie9.0.811385421 and firefox9.0.1 passed the test, I found it on this website --> http://site518.net/javascript-cross-domain-request/ :: is the client code, and the following is the service code string phoneno = request. getparameter ("phoneno"); // todo... response. setcontenttype ("application/JSON; charset = UTF-8"); response. getwriter (). print ("{customstate: 'hascustomed'}"); Note: when the server outputs data to the client, the output must be a JSON string, otherwise, the client cannot receive ========================================================== ===================-- %>

The next step is to use jquery to implement cross-origin asynchronous requests.

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <SCRIPT type = "text/JavaScript" src = "JS/jquery-1.7.1.min.js"> </SCRIPT> <SCRIPT type = "text/JavaScript" >$ (function () {$ ('# validatecustom '). click (function () {$. getjson ('HTTP: // 123. 125. **. * **: 8088/ecpaycus/web/getcustomizebyphoneno? Jsoncallback =? & Phoneno = 18601148104 ', function (JSON) {var customstate = JSON. customstate; alert ('server return result: '+ customstate) ;}) ;}</SCRIPT> <span style = "color: blue; text-Decoration: underline; cursor: pointer; "id =" validatecustom "> click here to complete customization </span> <% -- ================ ========================================================== ================================ note: in this example, ie9.0.811116421 and firefox9.0.1 are both tested and used: the client code is above, and the following is the server code string phoneno = request. getparameter ("phoneno"); // todo... string jsoncallback = request. getparameter ("jsoncallback"); string jsonreturn = "{customstate: 'hasmizmized'}"; response. setcontenttype ("application/JSON; charset = UTF-8"); response. getwriter (). print (jsoncallback + "(" + jsonreturn + ")"); Note: when the server outputs data to the client, the output must be a JSON string; otherwise, the client cannot receive and, when the client requests, when the callback function name must be provided and put in the form of parameters after the request URL and output by the server to the client, you must put the received callback function name in front of the JSON string ======================== ========================================================== =========================================-- %>

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.