------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------
This blog covers several ways to send HTTP requests across domains,post requests, get requests
Directory:
one, the use of JsonP ( can only be get)
two, using CROS ( need to be configured at one end of the receiver)
third, the use of form forms (sometimes there will be problems, a detailed description)
four, using Proxy website to help turn ( not recommended, unsafe, low performance , do not explain)
Five, back -end Java backend via net send
One,jsonp the way :
$.ajax ({ "http://localhost:9090/student", "GET" ,// Specifies the data type returned by the server function (data ) {var//JSON object to convert to a string $ ("#text"). Val (result); } });
It can only send a GET request
two,Cros way : ( The one end that needs to receive also configures )
1. Sending office :
$.ajax ({ "your URL which return JSON", "POST" ,true, Data:data, "JSON", success:function(result) { alert ( Json.stringify (Result)) , error:function(xhr,status,error) { alert ( status); } );
2. Receiving Office :
Response.AddHeader ("Access-control-allow-origin", "*""Access-control-allow-methods", "POST"" Access-control-max-age "," 1000 ");
Three, submitted by Form form, (can achieve post cross-domain request)
Encapsulates the data into a field in a form form, and then sends the past
Bad point: To join you in the Form form submission process, the new pop-up window is directly turned off by the code, it is possible that it did not pass through
functionCrossdomainpost () {//ADD The IFRAME with a unique name variframe = document.createelement ("iframe"); varuniquestring = "Change_this_to_some_unique_string"; Document.body.appendChild (IFRAME); Iframe.style.display= "None"; Iframe.contentWindow.name=uniquestring; //construct a form with hidden inputs, targeting the IFRAME varform = document.createelement ("form"); Form.target=uniquestring; Form.action= "Http://INSERT_YOUR_URL_HERE"; Form.method= "POST"; //repeat for each parameter varinput = document.createelement ("Input"); Input.type= "hidden"; Input.name= "Insert_your_parameter_name_here"; Input.value= "Insert_your_parameter_value_here"; Form.appendchild (input); Document.body.appendChild (form); Form.submit ();}
Four, the use of agents ( unsafe, poor performance , so do not speak)
Five, using background send (Java net)
Note: Do not start on the level of the transaction, it is possible that you have not persisted to the database at the time of the data, so the receiving end is not
PackageCom.xy.aider;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.URL;Importjava.net.URLConnection;Importjava.util.List;ImportJava.util.Map;/*** Background Send cross-domain post and get requests *@authorHeng Hui **/ Public classSendhttprequestutil {/*** A request to send a GET method to a specified URL *@paramURL * URL to send request *@paramparam * Request parameters, request parameters should be in the form of name1=value1&name2=value2. * @returnThe response result of the remote resource represented by the URL*/ Public Staticstring sendget (string url, string param) {string result= ""; BufferedReader in=NULL; Try{String urlnamestring= URL + "?" +param; URL Realurl=NewURL (urlnamestring); //opening and linking between URLsURLConnection connection =realurl.openconnection (); //to set common request propertiesConnection.setrequestproperty ("Accept", "*/*"); Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("User-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); //Alternative Scenarios//conn.setrequestproperty ("useragent", "mozilla/4.0" (compatible; MSIE 5.0; Windows NT; Digext) "); //establish an actual connectionConnection.connect (); //Get all response header fieldsmap<string, list<string>> map =Connection.getheaderfields (); //iterate through all the response header fields for(String key:map.keySet ()) {System.out.println (key+ "--->" +Map.get (key)); } //defines the response of the BufferedReader input stream to read the URLin =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); String Line; while(line = In.readline ())! =NULL) {result+=Line ; } } Catch(Exception e) {System.out.println ("Send GET request exception!" " +e); E.printstacktrace (); } //Use the finally block to close the input stream finally { Try { if(In! =NULL) {in.close (); } } Catch(Exception E2) {e2.printstacktrace (); } } returnresult; } /*** Request to send the Post method to the specified URL *@paramURL to send the request to *@paramparam The request parameter, the request parameter should be in the form of name1=value1&name2=value2. * @returnThe response result of the remote resource represented*/ Public Staticstring sendpost (string url, string param) {PrintWriter out=NULL; BufferedReader in=NULL; String result= ""; Try{URL Realurl=Newurl (URL); //opening and linking between URLsURLConnection conn =realurl.openconnection (); //to set common request propertiesConn.setrequestproperty ("Accept", "*/*"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); //Alternative Scenarios//conn.setrequestproperty ("useragent", "mozilla/4.0" (compatible; MSIE 5.0; Windows NT; Digext) "); //to send a POST request, you must set the following two linesConn.setdooutput (true); Conn.setdoinput (true); //1. Get the output stream corresponding to the URLConnection objectout =NewPrintWriter (Conn.getoutputstream ()); //2. Chinese garbled need to change printwriter to the following//out=new OutputStreamWriter (Conn.getoutputstream (), "UTF-8")//Send Request Parametersout.print (param); //buffer for flush output streamOut.flush (); //defines the response of the BufferedReader input stream to read the URLin =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ())); String Line; while(line = In.readline ())! =NULL) {result+=Line ; } } Catch(Exception e) {System.out.println ("Send POST Request exception!" "+e); E.printstacktrace (); } //Use the finally block to close the output stream, input stream finally{ Try{ if(out!=NULL) {out.close (); } if(in!=NULL) {in.close (); } } Catch(IOException ex) {ex.printstacktrace (); }} System.out.println ("Post Push results:" +result); returnresult; } ///Data similar to this write Public voidTestdemo () {//send a GET requestString s=sendhttprequestutil.sendget ("http://localhost:6144/Home/RequestString", "key=123&v=456"); System.out.println (s); //send a POST requestString sr=sendhttprequestutil.sendpost ("http://localhost:6144/Home/RequestPostString", "key=123&v=456"); System.out.println (SR); }}
Cross-domain sending of HTTP requests detailed