public class DydSearchHttp extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//response.setCharacterEncoding("gbk");
//System.out.println(response.getCharacterEncoding());
//获取前台页面传参
String unitName1 = request.getParameter("company");
String unitName=new String(unitName1.getBytes("utf-8"),"iso-8859-1");
System.out.println(unitName+"1111==============");
getCilentData(request,response,unitName);
}
public void getCilentData(HttpServletRequest request, HttpServletResponse response,String unitName ) {
HttpClient client = new HttpClient();
String url = "http://10.0.0.35:8080/kwcgfrk/nldoRunT.action?handler=eachInterface.kwLegalPersonInterface";
PostMethod postMethod = new PostMethod(url);
//设置参数编码为gbk
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
//构造键值对参数
System.out.println("unitName==="+unitName);
NameValuePair[] data = {
new NameValuePair("unitName", unitName)
};
// 把参数值放入postMethod中
postMethod.setRequestBody(data);
String strValue = "";
byte[] responseBody = null;
//执行
try {
int statusCode = client.executeMethod(postMethod);
if(statusCode !=HttpStatus.SC_OK){
System.out.println("Method failed: "+postMethod.getStatusLine());
}
//读取内容
responseBody = postMethod.getResponseBody();
System.out.println("responseBody=="+responseBody);
//处理内容
//System.out.println("2222"+new String(responseBody));
strValue = new String(responseBody,"utf-8");
System.out.println("strValue==="+strValue);
//return strValue;
} catch (HttpException e) {
// TODO Auto-generated catch block
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO传输出错了");
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
//strValue.toString();
//System.out.println(strValue.toString());
//解析返回的数据格式
if(!"".equals(strValue)){
String strFalg = strValue.substring(0, 1);
System.out.println(strFalg);
if("[".equals(strFalg)){
//如果是json格式,解析json
//StringBuffer returnValue = new StringBuffer();
//int num = 0;
JSONArray jsonObject = JSONArray.fromObject(strValue);
for(int i=0;i<jsonObject.size();i++){
JSONObject object = (JSONObject) jsonObject.get(i);
String ssqx=object.get("county").toString();
String company=object.get("unitName").toString();
String address=object.get("unitAddress").toString();
String relate=object.get("linkMan").toString();
String phone=object.get("telphone").toString();
String companytype=object.get("nature").toString();
System.out.println("object==="+object);
// System.out.println("unitName="+company+";ssqx="+ssqx+";address="+address+";relate="+relate+";phone="+phone);
}
//JSONObject jsonObject = JSONObject.fromObject(strValue);
System.out.println("json===="+jsonObject);
}else{
request.setAttribute("test", new String(responseBody));
}
}
//释放连接
postMethod.releaseConnection();
}
}
function getonload(){
var formObj = parent.document.getElementsByName("formid")[0].value;
var company=document.getElementsByName(formObj+'_COMPANY')[0].value;
alert(company);
//window.location.href("/"+getRootPath()+"/DydSearchHttp?company="+company);
$.ajax({
url:"/"+getRootPath()+"/DydSearchHttp", //访问路径
type:"POST", //访问方式
data:{"company":company}, //传入服务端的数据
dataType:"json",
//contentType:"application/json;charset=utf-8",
success :function(data) {
alert(data);
}
},
error:function(){
alert("加载信息失败");
}
});
}
我想通过ajax获取java代码中string类型的数据 ssqx company这些
5 个解决方案
#1
求大神指点啊
#2
怎么没人回复啊
#3
执行了哪个回调?success还是error。。自己看返回的json结构什么样的
#4
我觉得没有必要遍历啊,直接httpResponse.getWriter().write(JSONArray.fromObject(strValue))把数据返回到前台,然后
for(var i=0;i<data.length;i++){
alert(data[i].county);
alert(data[i].unitName);
}
或者直接把这两个String ssqx=object.get("county").toString();
String company=object.get("unitName").toString();
组成json串数组返回前台
for(var i=0;i<data.length;i++){
alert(data[i].county);
alert(data[i].unitName);
}
或者直接把这两个String ssqx=object.get("county").toString();
String company=object.get("unitName").toString();
组成json串数组返回前台
#5
以解决,谢谢
#1
求大神指点啊
#2
怎么没人回复啊
#3
执行了哪个回调?success还是error。。自己看返回的json结构什么样的
#4
我觉得没有必要遍历啊,直接httpResponse.getWriter().write(JSONArray.fromObject(strValue))把数据返回到前台,然后
for(var i=0;i<data.length;i++){
alert(data[i].county);
alert(data[i].unitName);
}
或者直接把这两个String ssqx=object.get("county").toString();
String company=object.get("unitName").toString();
组成json串数组返回前台
for(var i=0;i<data.length;i++){
alert(data[i].county);
alert(data[i].unitName);
}
或者直接把这两个String ssqx=object.get("county").toString();
String company=object.get("unitName").toString();
组成json串数组返回前台
#5
以解决,谢谢