The breakthrough of Ajax cross-domain Access restrictions

Source: Internet
Author: User
Tags set set

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

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.