------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
本篇博客讲述几种跨域发HTTP请求的几种方法,POST请求,GET请求
目录:
一,采用JsonP的方式(只能是GET)
二,采用CROS的方式(需要在接收的一端也有配置)
三,采用form表单的方式(有些时候会存在问题,一会详细说明)
四,采用代理网站帮忙转(不推荐,不安全,性能低,不做解释)
五,后台JAVA后端通过net方式发送
一,jsonP的方式:
???????????????$.ajax({ ???????????????????url: "http://localhost:9090/student", ???????????????????type: "GET", ???????????????????dataType: "jsonp", //指定服务器返回的数据类型 ???????????????????success: function (data) { ???????????????????????var result = JSON.stringify(data); //json对象转成字符串 ???????????????????????$("#text").val(result); ???????????????????} ???????????????});
它只能发送get请求
二,CROS的方式:(需要接收的一端也配置)
1.发送处:
???????$.ajax({ ???????????url: "your url which return json", ???????????type: "POST", ???????????crossDomain: true, ???????????data: data, ???????????dataType: "json", ???????????success:function(result){ ???????????????alert(JSON.stringify(result)); ???????????}, ???????????error:function(xhr,status,error){ ???????????????alert(status); ???????????} ???????});
2.接收处:
response.addHeader( "Access-Control-Allow-Origin", "*" ); response.addHeader( "Access-Control-Allow-Methods", "POST" ); response.addHeader( "Access-Control-Max-Age", "1000" );
三,采用Form表单的方式提交,(可以实现POST跨域请求)
把数据封装成form表单中的字段,然后发送过去
不好的点:加入你在form表单提交的过程中,这个新增弹窗被代码给直接关掉,有可能的是它没有传过去
function crossDomainPost() { ?// Add the iframe with a unique name ?var iframe = document.createElement("iframe"); ?var uniqueString = "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 ?var form = document.createElement("form"); ?form.target = uniqueString; ?form.action = "http://INSERT_YOUR_URL_HERE"; ?form.method = "POST"; ?// repeat for each parameter ?var input = 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();}
四,采用代理(不安全,性能不好,所以不讲)
五,采用后台发送(Java的net)
注意的点:不要在事务开启的那层发,有可能你发数据的时候还没持久化到数据库,所以接收的那一端没有
package com.xy.aider;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Map;/** * 后台发送跨域的post和get请求 * @author Heng Hui * */public class SendHttpRequestUtil { ???/** ????* 向指定URL发送GET方法的请求 ????* ?????* @param url ????* ???????????发送请求的URL ????* @param param ????* ???????????请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 ????* @return URL 所代表远程资源的响应结果 ????*/ ???public static String sendGet(String url, String param) { ???????String result = ""; ???????BufferedReader in = null; ???????try { ???????????String urlNameString = url + "?" + param; ???????????URL realUrl = new URL(urlNameString); ???????????// 打开和URL之间的连接 ???????????URLConnection connection = realUrl.openConnection(); ???????????// 设置通用的请求属性 ???????????connection.setRequestProperty("accept", "*/*"); ???????????connection.setRequestProperty("connection", "Keep-Alive"); ???????????connection.setRequestProperty("user-agent", ???????????????????"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); ???????????????????????//备用方案 ???????????//conn.setRequestProperty("UserAgent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); ???????????// 建立实际的连接 ???????????connection.connect(); ???????????// 获取所有响应头字段 ???????????Map<String, List<String>> map = connection.getHeaderFields(); ???????????// 遍历所有的响应头字段 ???????????for (String key : map.keySet()) { ???????????????System.out.println(key + "--->" + map.get(key)); ???????????} ???????????// 定义 BufferedReader输入流来读取URL的响应 ???????????in = new BufferedReader(new InputStreamReader( ???????????????????connection.getInputStream())); ???????????String line; ???????????while ((line = in.readLine()) != null) { ???????????????result += line; ???????????} ???????} catch (Exception e) { ???????????System.out.println("发送GET请求出现异常!" + e); ???????????e.printStackTrace(); ???????} ???????// 使用finally块来关闭输入流 ???????finally { ???????????try { ???????????????if (in != null) { ???????????????????in.close(); ???????????????} ???????????} catch (Exception e2) { ???????????????e2.printStackTrace(); ???????????} ???????} ???????return result; ???} ????/** ????* 向指定 URL 发送POST方法的请求 ????* @param url 发送请求的 URL ????* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 ????* @return 所代表远程资源的响应结果 ????*/ ???public static String sendPost(String url, String param) { ???????PrintWriter out = null; ???????BufferedReader in = null; ???????String result = ""; ???????try { ???????????URL realUrl = new URL(url); ???????????// 打开和URL之间的连接 ???????????URLConnection conn = realUrl.openConnection(); ???????????// 设置通用的请求属性 ???????????conn.setRequestProperty("accept", "*/*"); ???????????conn.setRequestProperty("connection", "Keep-Alive"); ???????????conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); ???????????????????????//备用方案 ???????????//conn.setRequestProperty("UserAgent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); ???????????// 发送POST请求必须设置如下两行 ???????????conn.setDoOutput(true); ???????????conn.setDoInput(true); ???????????//1.获取URLConnection对象对应的输出流 ???????????out = new PrintWriter(conn.getOutputStream()); ???????????//2.中文有乱码的需要将PrintWriter改为如下 ???????????//out=new OutputStreamWriter(conn.getOutputStream(),"UTF-8") ???????????// 发送请求参数 ???????????out.print(param); ???????????// flush输出流的缓冲 ???????????out.flush(); ???????????// 定义BufferedReader输入流来读取URL的响应 ???????????in = new BufferedReader(new InputStreamReader(conn.getInputStream())); ???????????String line; ???????????while ((line = in.readLine()) != null) { ???????????????result += line; ???????????} ???????} catch (Exception e) { ???????????System.out.println("发送 POST 请求出现异常!"+e); ???????????e.printStackTrace(); ???????} ???????//使用finally块来关闭输出流、输入流 ???????finally{ ???????????try{ ???????????????if(out!=null){ ???????????????????out.close(); ???????????????} ???????????????if(in!=null){ ???????????????????in.close(); ???????????????} ???????????} ???????????catch(IOException ex){ ???????????????ex.printStackTrace(); ???????????} ???????} ???????System.out.println("post推送结果:"+result); ???????return result; ???} ???????///数据类似这种写入 ???public void testDemo(){ ???????//发送 GET 请求 ???????String s=SendHttpRequestUtil.sendGet("http://localhost:6144/Home/RequestString", "key=123&v=456"); ???????System.out.println(s); ???????????????//发送 POST 请求 ???????String sr=SendHttpRequestUtil.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456"); ???????System.out.println(sr); ???}}
跨域发送HTTP请求详解
原文地址:https://www.cnblogs.com/DawnCHENXI/p/9261477.html