Ajax cross-domain access encounters this exception:
XMLHttpRequest cannot loadhttp://10.43.15.23/hnsh/rest/waterinfo/getriverinfo.do. No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' http://localhost:8080 ' is therefore not allowed access.
The usual practice is to use the server (Tomcat Java) to jump the request:
public static string Getjson (string url) {string path = URL; List List = new ArrayList (); String content = "";//Read URL contents Set set = new HashSet (); try {url cumturl = new URL (path); URLConnection cumtconnection = cumturl.openconnection ();D atainputstream din = new DataInputStream ( Cumtconnection.getinputstream ()); String inputline= ""; while ((Inputline = Din.readline ()) = null) {content + = Inputline;} Content = new String (content.getbytes ("iso-8859-1"), "UTF-8"). Trim ();d in.close (); catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;} return content;}
In this page (jquery):
$ ("#b2"). Click (function () { $.getjson ("Http://ip:8080/hnsh/rest/getInfo.do", function (data,status) { Alert ("Data:" + data.list.length + "\ n Status:" + status); }) ;
If you use
JsonpTo do a direct cross-domain, add the following URL: Yoururl?callback=?,jquery native support,
In this page (jquery):
$ ("#b2"). Click (function () { $.getjson ("http://10.43.15.23/hnsh/rest/waterinfo/getriverinfo.do<strong> <span style= "color: #ff6666;" >?callback=?</span></strong>, Function (data,status) { alert ("Data:" + data.list.length + "\ n Status:" + status);}) ;
In this case, you still need to modify the server to support callback; see (see: http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/)
Otherwise, it will parse the error if it is requested to the exotic data:
Uncaught syntaxerror:unexpected Token:
There is also a way to turn off the browser to cross-domain security restrictions, that is, the "Disable Web Security" page and the server what code does not move:
In Firefox, for example, you can install an extension component:
Extension Address: http://spenibus.net/files/gitbin/cors-everywhere-firefox-addon/
Restart the browser to enable the extension in the customization:
Find this component, drag it behind the search bar, click Enable, and then say goodbye to the cross-domain restrictions:
The breakthrough of Ajax cross-domain Access restrictions