[英]Post XML to jersey rest webservice
我试图将消息从facelet(.xhtml)页面发布到REST Web服务(泽西岛)。 我认为如果服务器拥有的话,可以在javascript / jQuery中进行操作,例如
<?php header('Access-Control-Allow-Origin: *'); ?>
但是服务器似乎没有这样的属性,而且我不知道在配置中在哪里修改它。
我试过了
var settings = {
"async": true,
"crossDomain": true,
"url": "example.com",
"method": "POST",
"headers": {
"content-type": "application/xml",
"cache-control": "no-cache"
},
"data":
"<consultation>\n \n\
<consultationDescription>"+description+"</consultationDescription>\n \n\
<customerName>"+fullName+"</customerName>\n \n\
<customerPhone>"+phonenumber+"</customerPhone>\n \n\
<endDateAndTime>"+endDateAndTime+"</endDateAndTime>\n \n\
<startDateAndTime>"+startDateAndTime+"</startDateAndTime>\n\n\
</consultation>",
contentType: "application/xml",
$.ajax(settings).done(function (response) {
console.log(response);
});
但是我只会得到跨原点错误。
因此,我应该尝试找到在服务器上修改源的位置,还是应该采用其他方法? 也许<h:form>并通过像#{sendXML.someMethod}之类的javabean发布,但是我不知道在哪里可以找到它的语法。 我已经被这个问题困扰了一段时间,找不到一个好的答案。 你们有什么想法吗?
示例: http : //postimg.org/image/5k2thyl3p/
单击绿色单元格->写消息(提交)->在服务器上预订时间。 更新视图。
因此,我按照以下指南找出了解决方法: http : //www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
private final String USER_AGENT = "Mozilla/5.0";
// HTTP POST request
private void sendPost() throws Exception {
try {
String url = "http://myurl";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "UTF-8");
con.setRequestProperty("content-type", "application/xml");
String urlParameters = "<myXML></myXML>";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
catch (Exception e) {
e.printStackTrace();
}
}
由于javascript由于跨域错误而无法正常工作,因此在Javabean中进行工作就可以了。
我通过将Http 的 URLConnection修改为HttpURLConnection来更改了代码示例,并添加了con.setRequestProperty(“ content-type”,“ application / xml”)。
我还用try / catch封装了该语句。
我可能从一开始就不好说我的问题。 但是通过在bean中进行操作,它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.