Ajax cross-domain implementation code (background JSP)

AJAX Tutorial AJAX = Asynchronous JavaScript and XML (asynchronous JavaScript and XML).

At the time of application, it is mainly to create an XMLHttpRequest object, and the specified service address is called.

However, the various versions of IE are not only the same, so it may be specially processed when creating a sub-object.

function createXmlhttpRequest () {var xmlhttp; try {xmlhttp = new xmlhttpRequest (); // IE7 and above, other browser } catch (e) {Try {xmlhttp = new activiXObject (“msxml2.xmlhttp”); // ie6} catch (e) {Try {xmlhttp = new activXObject (“Microsoft.xmlhttp”); // i i 以下 以下}} Catch ( e) {throw “Create a JAX Object Failed!”;}}} Return XMLHTTP; Xmlhttp.send (null); xmlhttp.onreadyStateChange = function (result) {if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {ALTER (RESULT.TEST);}};
  However, when the browser performs JavaScript code, there is a famous homologous policy, which makes the cross-domain request is not so convenient.  
What is usually used to support cross-domain?
1, through the intermediate proxy server, acquire data to be across domains.

2, through the IFRAME embedding with the page of the request domain, to solve cross-domain access issues.

3, through JSONP mode.

4, but now XMLHttpRequest Level2 (XHR2) allows cross-domain requests, but the declaration to display the declaration to allow cross-domain requests in the backchaop of Server (support for browsers: http: // caniuse. COM / # feat = xhr2). JSONP and XTR2 will be simply below.

JSONP:

JSONP is simple to say that the

tag is used to implement the call of cross-domain requests because the loading of the script in the browser is not affected by homologous strategies. .

Function GET () {var URL = ‘http: // localhost: 8080 / simpleblog / ajaxtest? Callback = Callback’; var script = document.createElement (‘script ‘); Script.SetaTRibute (“Type”, “text / javascript”); script.src = url; document.body.Appendchild (script);} Function Callback (VA) {Alert (va.tEST);}

Service (Java):

Boolean Jsonp = FALSE; String CB = THIS. Request.getParameter (“callback”); if (cb! = null) {jsonp = true; response.setContentType (“text / javascript”);} else {response.setContentType (“Application / X-JSON”);} PrintWriter OUT = response.getwriter (); if (jsonp) {try {out.println (CB + “({\” Test \ “: \” 1 \ “})”); out.flush (); out.close );} Catch (Exception E) {Throw E;}}

This can achieve a cross-domain call.
And the jQuery we often has implemented such a package, which is simpler.
   $ (docuument) .ready (Function () {$ ('# jqueryajax'). Bind ('click ", function () {$ .ajax ({type : 'get', Async: False, URL: 'http: // localhost: 8080 / simpleblog / ajaxtest1', DataType: 'Jsonp', JSONP: 'CALLBACK ', SUCCESS: FUNCTION (JSON) {Alert (json.result);}, error: function () {Alert (' five ');}};});}); 

Server (Java):

I used Struts to write:
  Public class ajaxtest1 Extends ActionSupport {Private String Result Public string getResult () {return result;} public string execute () {this.result = "1"; return "jQueryajax";}}  

Profile:

Callback
   

Say XTR2:

This is more simple, creating a call directly.
  Function CreateCorsRequest (Method, URL) {var xhr = new xmlhttpRequest (); if ('withcredentials' in xhr) {xhr.open (Method, URL, TRUE) } else if (TypeOf xdomainRequest! = 'undefined') {XHR = new xdomainRequest (); xhr.open (Method, URL);} else {xhr = null;} Return Xhr;} Function XHR2 () {Var Request = CreateCorsRequest ('get', 'http: // localhost: 8080 / Simpleblog / ajaxtest1 '); if (request) {request.onload = function () {Alert (Request.ResponseText);} Request.onerror = function (e) {Alert (' error ');} request.send ()  
Server: Actually, as long as
httpresponse.addhead (“Access-Control-Allow-Origin”, “*”) is set in return to Response.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment