ajax用jquery问题调用jax-rs [英] ajax call to jax-rs with jquery issue

查看:151
本文介绍了ajax用jquery问题调用jax-rs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用post方法调用消耗json对象的Web服务。我做了然后它再也无法工作不知道是什么问题。

这里是我的方法

I'm trying to call a Webservice that consumes a json object with post method .I did it then It wont work again don't know what is the problem.
here is my method

@POST
@Path("/post")
@Consumes("application/json")
@Produces("application/json")
public Response testClient(Client c) throws IOException {
    System.out.println(c.getAdresseCl());
    ResponseBuilder builder = Response.ok(c.getAdresseCl());
    builder.header("Access-Control-Allow-Origin", "*");
    builder.header("Access-Control-Max-Age", "3600");
    builder.header("Access-Control-Allow-Methods", "*");
    builder.header(
            "Access-Control-Allow-Headers",
            "X-Requested-With,Host,User-Agent,Accept,Accept-Language,Accept-Encoding,Accept-Charset,Keep-Alive,Connection,Referer,Origin");
    return builder.build();
}

调用此我用过这个

$.ajax({
  type: 'POST',
  url: "http://localhost:9080/FournisseurWeb/jaxrs/clients/post",
  data: '{"adresseCl":"tunis"}',
  dataType:'json',
  contentType: "application/json; charset=utf-8", 
  success: function (msg) {
    alert(msg);
  },

  error: function (xhr, ajaxOptions, thrownError) {
    alert('error');
  }

});

我注意到当我将contentType设置为application / json时,方法将更改为OPTIONS。
,当我不使用内容类型时,我得到415不支持的媒体类型我不知道如何解决这个问题。我没有结果通过太​​多时间:(谢谢你帮助我

well I remark that when I set the contentType to application/json the method changes to OPTIONS . and when I don't use the content type I got "415 Unsupported Media Type " I dont know how to fix this. I passed too much time without results :(
thank you for helping me

推荐答案

这是消耗一个方法的方法文本xml fomat并将其映射到一个对象以保持下一个

this is the method that consumes a text xml fomat and map it to an object to persist it next

@POST
@Path("/inscription")
@Produces(MediaType.TEXT_HTML)
public Response testClient(String s) {
    ResponseBuilder builder = null;

    try {

        final String xmlString = s;
        final StringReader xmlReader = new StringReader(xmlString);
        final StreamSource xmlSource = new StreamSource(xmlReader);
        final JAXBContext jaxbContext = JAXBContext
                .newInstance(Client.class);
        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        final Client client = (Client) unmarshaller.unmarshal(xmlSource,
                Client.class).getValue();
        System.out.println("nomCl  : " + client.getNomCl());
        System.out.println("prenomCl  : " + client.getPrenomCl());
        System.out.println("emailCl  : " + client.getEmailCl());
        System.out.println("numTel  : " + client.getNumTel());
        System.out.println("long_  : " + client.getLong_());
        System.out.println("lat  : " + client.getLat());
        System.out.println("LoginCl  : " + client.getLoginCl());
        System.out.println("PasswordCl  : " + client.getPasswordCl());
        System.out.println("adresseCl  : " + client.getAdresseCl());
        EntityManagerFactory factory;
        factory = Persistence.createEntityManagerFactory("FournisseurWeb");
        EntityManager em = factory.createEntityManager();
        em.getTransaction().begin();
        em.persist(client);
        em.getTransaction().commit();
        em.close();
        factory.close();
        builder = Response.ok("true");
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        builder = Response.ok("false");
        builder.header("Access-Control-Allow-Origin", "*");
        builder.header("Access-Control-Max-Age", "3600");
        builder.header("Access-Control-Allow-Methods", "POST");
        builder.header(
                "Access-Control-Allow-Headers",
                "X-Requested-With,Host,User-Agent,Accept,Accept-Language,Accept-Encoding,Accept-Charset,Keep-Alive,Connection,Referer,Origin");
        return builder.build();
    }
    builder.header("Access-Control-Allow-Origin", "*");
    builder.header("Access-Control-Max-Age", "3600");
    builder.header("Access-Control-Allow-Methods", "POST");
    builder.header(
            "Access-Control-Allow-Headers",
            "X-Requested-With,Host,User-Agent,Accept,Accept-Language,Accept-Encoding,Accept-Charset,Keep-Alive,Connection,Referer,Origin");
    return builder.build();
}

我用这个样本用ajax来调用这个方法:

I use to call this method using ajax with this sample :

var x="<client><nomCl>Taarit</nomCl><prenomCl>Aymen</prenomCl><emailCl>aymen.taarit@gmail.com</emailCl><numTel>222</numTel><long_>1.66</long_></client>";
$.ajax({
        url: 'http://localhost:9080/FournisseurWeb/jaxrs/clients/cl',
        type: 'post',
        scriptCharset: "utf-8" ,
        dataType:"xml",
        data: x,
        success: function(data, status) {  
        console.log(data);         
        }
    });

这是一个jax-rs调用,使用跨域的ajax POST,所以希望它有帮助:)

this is a jax-rs call with ajax POST using cross domain so hope that it helps :)

注意:没有JSONP的跨域调用在这里是合法的,因为服务器返回以下标头,这启用了跨域AJAX!

 builder.header("Access-Control-Allow-Origin", "*");

参见关于Access-Control-Allow-Origin的Mozilla开发人员中心页面了解更多详情。

这篇关于ajax用jquery问题调用jax-rs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆