jquery 跨域解决方案1

  1. package cn.com.ld.util;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.io.InputStreamReader;   
  7. import java.net.HttpURLConnection;   
  8. import java.net.URL;   
  9.   
  10. /**  
  11.  * @filename: HttpAccessProxy  
  12.  * @Description: TODO  
  13.  * @author java小生  
  14.  * @date 2012-9-13 下午4:09:56  
  15.  */  
  16. public class HttpAccessProxy {   
  17.     public static String accessProxy(String pageUrl) {   
  18.         URL url;   
  19.         String pageString = "";   
  20.         InputStream is = null;   
  21.         BufferedReader br = null;   
  22.         StringBuffer sb = null;   
  23.         try {   
  24.             url = new URL(pageUrl);   
  25.             HttpURLConnection connection = (HttpURLConnection) url   
  26.                     .openConnection();   
  27.             is = connection.getInputStream();   
  28.             br = new BufferedReader(new InputStreamReader(is));   
  29.             sb = new StringBuffer();   
  30.             String line = null;   
  31.             while ((line = br.readLine()) != null) {   
  32.                 sb.append(line + "\n");   
  33.             }   
  34.             pageString = sb.toString();   
  35.         } catch (Exception e) {   
  36.             e.printStackTrace();   
  37.         } finally {   
  38.             try {   
  39.                 is.close();   
  40.                 br.close();   
  41.             } catch (IOException e) {   
  42.                 e.printStackTrace();   
  43.             }   
  44.   
  45.         }   
  46.         return pageString;   
  47.     }   
  48.   
  49.     public static void main(String[] args) {   
  50.         System.out.println(HttpAccessProxy.accessProxy("http://localhost:8080/ws/web/pages/data/json.jsp"));   
  51.     }   
  52. }  
package cn.com.ld.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @filename: HttpAccessProxy
 * @Description: TODO
 * @author java小生
 * @date 2012-9-13 下午4:09:56
 */
public class HttpAccessProxy {
	public static String accessProxy(String pageUrl) {
		URL url;
		String pageString = "";
		InputStream is = null;
		BufferedReader br = null;
		StringBuffer sb = null;
		try {
			url = new URL(pageUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			is = connection.getInputStream();
			br = new BufferedReader(new InputStreamReader(is));
			sb = new StringBuffer();
			String line = null;
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
			pageString = sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return pageString;
	}

	public static void main(String[] args) {
		System.out.println(HttpAccessProxy.accessProxy("http://localhost:8080/ws/web/pages/data/json.jsp"));
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
跨域是指浏览器限制了由一个域名下的网页向另一个域名下的服务器发送请求的行为。这是为了保护用户的隐私和安全而设立的安全策略。然而,在某些情况下,我们可能需要在前端使用jQuery的ajax方法发送跨域请求。为了解决这个问题,可以使用JSONP或CORS进行跨域请求。 JSONP(JSON with Padding)是通过在请求中添加一个回调函数的方式来实现跨域请求的。回调函数会将服务端返回的数据包裹在函数中,从而解决了跨域问题。通过设置dataType为"jsonp",可以告诉jQuery使用JSONP来发送跨域请求。例如: ```javascript $.ajax({ url: "http://example.com/api/data", dataType: "jsonp", success: function(response) { console.log(response); } }); ``` CORS(Cross-Origin Resource Sharing)是一种更为现代化的跨域请求解决方案。通过在服务端设置响应头中的Access-Control-Allow-Origin字段,允许指定的域名来访问资源。如果服务端允许跨域请求,那么在前端使用jQuery的ajax方法发送请求时,就不再会受到浏览器的限制。例如: ```javascript $.ajax({ url: "http://example.com/api/data", type: "GET", success: function(response) { console.log(response); } }); ``` 总的来说,如果需要在前端使用jQuery的ajax方法进行跨域请求,可以使用JSONP或CORS来解决跨域问题。具体的解决方案取决于服务端是否支持和设置了相应的跨域策略。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [jQuery(五)Ajax、跨域](https://blog.csdn.net/weixin_53072519/article/details/120292253)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [jquery中ajax处理跨域的三大方式](https://blog.csdn.net/lgxzzz/article/details/119900540)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值