亲们,我是前端,后台给了我url,我不知道要怎么获取到后台数据,谁能教我一下,我是本地,后台的url已经传到服务器
public static String httpPost(String url, List<NameValuePair> params) {
String ret = null;
String uriAPI = url;
HttpClient httpClient = null;
HttpPost httpRequest = new HttpPost(uriAPI);
try {
// 设置编码格式
HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
// 绑定数据
httpRequest.setEntity(entity);
// 得到请求对象
httpClient = new DefaultHttpClient();
// 设置请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
// 设置读取超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);
// 取得HTTP response
HttpResponse httpResponse = httpClient.execute(httpRequest);
// 若状态码为200 ok
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 取出回应字串
ret = EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (ConnectTimeoutException cte) {
Log.d("err","与服务器建立连接超时");
cte.printStackTrace();
httpClient.getConnectionManager().shutdown();
return "connect_timeout";
} catch (SocketTimeoutException ste) {
Log.d("err", "从服务器获取响应数据超时");
ste.printStackTrace();
httpClient.getConnectionManager().shutdown();
return "response_timeout";
} catch (IOException e) {
// 出现这个异常有三种情况
//1.没开网络
//2.没网络权限
//3.网络加载延时,网络状态不行
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
用AJax传,根据请求是get还是post方法.格式百度下就行,我是后端,所以前端代码有时候忘记。
跨域问题一般用jsonp,不知道你做的是pc端还是移动端页面,jquery和移动端的zepto的ajax挺好用的
ajax等
LZ 狠补基础吧
想清楚原理,需要学习java web的框架,你可以在前端、后端两个位置查看数据,servlet配对,就能将数据传到页面,例如数据是一个list,页面显示,一般使用foreach、jstl标签来展示${list.name},这些数据你作为前端,很容易用浏览器的调试台调试获取。
$.ajax({
type:"get",
url:"",//这个是后端传给你的url地址
async:true,
data: { },//这里面传你要给后端的数据,通过这个后端传给你你想要的数据
dataType:"jsonp",
success:function(data){
//data是你从后端获取的数据
}
});