Envíe la solicitud de publicación a otro servidor usando ajax (dominio cruzado) y use el código jsp como proxy

I need to get json data from the company server. The interface they give to me is to send a post request with the proper params. And then I will get json data.

I use ajax to send a post request to local server and use jsp to send the post to the company server. and I put the .html and .jsp in the same file named test. Then put the test file in the webapps file under Tomcat. But all I receive is httpstatus 500 error I tried to send the post in the java program, and I got the right answer.

Aquí está mi código.

Is there anything wrong with the code, or with the configuration? I'm a newer in web development and some mistakes may be obvious, but I try so much and can't get what I want.

[code=JScript][/code]
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function loadJson()
{
var xmlhttp = false;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  if (XMLHttpRequest.overrideMimeType)
  {
  XMLHttpRequest.overrideMimeType("text/xml");
  }
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if(!xmlhttp)
 {
window.alert("can't create object");
return false;
}   
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
  document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
  else
document.getElementById("myDiv").innerHTML=xmlhttp.status+"-"+xmlhttp.readyState;
  }
xmlhttp.open("POST","proxy.jsp",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   
  xmlhttp.send("url=http://aqueduc.kelcode.com/proc/gw.php&requestName=getIntervention&uid=UID_GATEWAY&startDate=1292223600&endDate=1408330000");
}
</script>

[code=HTML][/code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<button type="button" onclick="loadJson()">request data</button>
</body>
</html>

proxy.jsp
[code=Java][/code]
<%@ page import="java.net.*,java.util.*,java.lang.*,java.io.*"%>
<%@ page contentType="text/xml;charset=gb2312"%>
<%   
String url = null;
StringBuffer params = new StringBuffer();
Enumeration enu = request.getParameterNames();
int total = 0;
while (enu.hasMoreElements()) {
String paramName=(String)enu.nextElement();
if(paramName.equals("url")){
  url=request.getParameter(paramName);
}else{
  if(total==0){
      params.append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
  } else {
  params.append("&").append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
  }
  ++total;
}
}
String para = params.toString();
//out.println(url);
if(url != null){
// POST request
URL connect = new URL(url.toString());
URLConnection connection = connect.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
connection.connect();
DataOutputStream os = new DataOutputStream(connnection.getOutputStream());
os.write("requestName=getIntervention&uid=UID_GATEWAY&startDate=1292223600&endDate=1408330000");
os.flush();
os.close();

BufferedReader reader = new BufferedReader(new  InputStreamReader(connection.getInputStream()));
String line;
while((line = reader.readLine()) != null){
  out.println(line);
}
reader.close();
}
%>

preguntado el 31 de enero de 12 a las 16:01

Why aren't you using a library like jQuery or Prototype to smooth over the Ajax crud for you? There is no sense reinventing the wheel. -

A friend of mine told me to do this with Ajax. And then I learned it. Could you give me more details about that? Thank you very much. -

Actually, it's not the point. Could you tell me that is anything wrong with my jsp code? -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.