简体   繁体   中英

Post XML to jersey rest webservice

i'm trying to post a message from a facelet (.xhtml) page to a REST web service (Jersey). I think it would be possible to do within javascript/jQuery if the server would have, let's say

<?php header('Access-Control-Allow-Origin: *'); ?>

However the server seems to not have a property like that, and I don't know where to modify that in the config.

I tried this;

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);
});

But I only get cross origin error.

So should I try to find where to modify the origin on the server, or should I go with a different approach? Maybe a < h:form > and post via a javabean like #{sendXML.someMethod} etc, but I don't know where to find the syntax for that. I've been stuck with this problem for some time and can't find a good answer. Do you guys have any idee what to do?

Example: http://postimg.org/image/5k2thyl3p/

Click green cell --> Write message (submit) --> book time on server. Update view.

So I figured out how to do it by following this guide: 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();
}
}

Since javascript did not work due to Cross Domain error, doing the work in a Javabean worked just fine.

I changed the code example by modifying Http s URLConnection to a HttpURLConnection and added con.setRequestProperty("content-type", "application/xml").

I also wrapped the statment with a try/catch.

I probably stated my question poorly from the beginning. But by doing the operations in the bean it worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM