java 跨域

时间:2021-10-13 10:48:57

jsonp做前端跨域需要服务器的支持的,造成json字符串前缀 var a=...或者 a[].... 实在有点麻烦,故还是后台跨域取数据好了

package com.pro.domain;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; public class Test{ Log log = LogFactory.getLog(getClass());
static String smsurl = "http://www.jeasyui.com/demo/main/get_users.php"; public String sendShortMessage(String smsSrc, String mobileNo) { String line="";
StringBuffer sb = new StringBuffer(); try{
URL u = new URL(smsSrc);
URLConnection uc = u.openConnection(); PrintWriter out = new PrintWriter(uc.getOutputStream());
out.print("smsSrc=" + smsSrc + "&mobileNo=" + mobileNo);
out.close();//页面上输出的 BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); while ((line = in.readLine()) != null) sb.append(line + "\n"); in.close(); System.out.println(sb); } catch (Exception e) { log.error(e); } return new String(sb);
} public static void main(String[] args) {
Test test =new Test();
test.sendShortMessage(smsurl, "hello");
}
}