我在Angular 2上开发了web-app.Angular-code:
class RouteService {
private url : string = 'http://gbsapod/sapodbridge/getRoute?';
constructor(private http:Http) { }
get(cont, foot, out_station_code, dest_station_code, out_country_code, dest_country_code, special_marks_code, dispatch_type : string): Observable<Route> {
let buf_url_ : string = this.url;
// добавление параметров
buf_url_ = buf_url_ + "cont=" + cont;
buf_url_ = buf_url_ + "&foot=" + foot;
buf_url_ = buf_url_ + "&outstationcode=" + out_station_code;
buf_url_ = buf_url_ + "&deststationcode=" + dest_station_code;
buf_url_ = buf_url_ + "&outcountrycode=" + out_country_code;
buf_url_ = buf_url_ + "&destcountrycode=" + dest_country_code;
buf_url_ = buf_url_ + "&specialmarkscode=" + special_marks_code;
buf_url_ = buf_url_ + "&dispatchtype=" + dispatch_type;
return this.http.get(buf_url_, options ).map((resp:Response)=>{ let r =
resp.json() as Route; return r;});
Backend是Apache Tomcat下的servlet。 servlet源:
public class getRoute extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.addHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "*");
response. setCharacterEncoding("UTF-8");
String host = "gcsapod";
String port = "2080";
String in_login = null;
String in_password = null;
boolean isRMIHttp = false;
in_login = request.getParameter("login");
in_password = request.getParameter("password");
LogonInfo log = Session.createSession(host, port, in_login, Password.getEncryptedPassword(in_password, false, 0), isRMIHttp);
DistanceService ds = new DistanceService();
ds.in_kol_kont = Integer.parseInt(request.getParameter("cont"));// 0
ds.in_futov = Integer.parseInt(request.getParameter("foot")); //0
ds.in_st_otprav = request.getParameter("outstationcode"); //150000
ds.in_st_nazn = request.getParameter("deststationcode"); //320204
ds.in_str_otprav = request.getParameter("outcountrycode"); //112
ds.in_str_nazn = request.getParameter("destcountrycode"); //204
ds.in_os_cond2 =
Integer.parseInt(request.getParameter("specialmarkscode")); // по идее 58
ds.in_svo_code = (short)Integer.parseInt(request.getParameter("dispatchtype")); // код вида отправки - нормально сделать
ds.connectAppServer();
JsonObjectBuilder js_ = Json.createObjectBuilder();
if (ds.calcDistance()) {
ParagrafService paraf = new ParagrafService();
// выдача ответа
js_.add("error", "");
js_.add("out_joint_code", ds.out_vyh_styk);
js_.add("out_joint_name", ds.out_vyh_styk2);
js_.add("in_joint_code", ds.out_vh_styk);
js_.add("bel_dist", ds.out_localDistField);
js_.add("total_dist", ds.out_globalDistField);
js_.add("paragraf", paraf.get(ds.in_st_nazn));
JsonArrayBuilder js__ = Json.createArrayBuilder();
for (int i = 0; i < ds.distanceVector.size(); i++) {
Station tempStation = (Station) ds.distanceVector.elementAt(i);
JsonObjectBuilder js2_ = Json.createObjectBuilder();
js2_.add("kod", tempStation.kod);
js2_.add("name", tempStation.name);
js2_.add("dorName", tempStation.dorName);
js2_.add("dist", new Integer(tempStation.currentDistance).toString());
JsonObject js = js2_.build();
js__.add(js);
}
js_.add("route", js__);
JsonObject js = js_.build();
StringWriter stringWriter = new StringWriter();
JsonWriter writer = Json.createWriter(stringWriter);
writer.writeObject(js);
writer.close();
PrintWriter pw_ = response.getWriter();
pw_.println(stringWriter.getBuffer().toString());
pw_.close();
} else {
js_.add("error", ds.ErrorMessage);
js_.add("out_joint_code", "");
js_.add("out_joint_name", "");
js_.add("in_joint_code", "");
js_.add("bel_dist", "");
js_.add("total_dist", "");
js_.add("paragraf", "");
js_.add("route", "");
}
}
Servlet和前端部署在不同的服务器上。 Apache Tomcat上有CORS。在控制台(浏览器开发工具)中出现错误:
XMLHttpRequest cannot load http://gbsapod/sapodbridge/getRoute?
cont=0&foot=0&outstationcode=150000&des…
rycode=804&specialmarkscode=58&dispatchtype=1.
The 'Access-Control-Allow-Origin' header contains multiple values
'http://sapod_test.gbas.gomel.rw, *', but only one is allowed. Origin
'http://sapod_test.gbas.gomel.rw' is therefore not allowed access.
我做错了什么?感谢。