jQuery不能将URL参数发布到Jersey服务吗? [英] jQuery not POSTing URL arguments to Jersey service?

查看:75
本文介绍了jQuery不能将URL参数发布到Jersey服务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来很简单,但是我不知道为什么它不起作用.

This seems so simple, but I can't figure out why it doesn't work.

我定义了一个Jersey服务:

I have a Jersey service defined:

@POST
public SomeObject createSomeObject(
    @QueryParam("firstParam") String firstParam,
    @QueryParam("secondParam") String secondParam)
{
    // Does stuff, returns the object.
}

现在,如果我做一个简单的curl,它就可以正常工作:

Now, if I do a simple curl, it works fine:

curl -X POST "http://localhost:8080/path/to/service?firstParam=Bleh&secondParam=Blah

但是,Java中的firstParam和secondParam在null中产生以下结果:

However, the following results in null for firstParam and secondParam in the Java:

$.ajax({
    url: '/path/to/service',
    type: 'POST',
    data: {
        firstParam: 'Bleh',
        secondParam: 'Blah'
    },
    success: doStuff,
    error: eek
});

这似乎很简单.我觉得他们的行为应该完全一样.我想念什么?我尝试过的事情(但似乎没有必要):

This seems ridiculously straight-forward. I feel they should behave exactly the same. What am I missing? Things I've tried (but don't seem necessary):

  • contentType: 'application/x-www-form-urlencoded'添加到ajax调用中.
  • @Consumes(MediaType.APPLICATION_FORM_URLENCODED)添加到Jersey服务.
  • JSON.stringify包装数据对象(我知道在contentType: 'json'时需要这样做,但不必在这里.
  • Adding contentType: 'application/x-www-form-urlencoded' to the ajax call.
  • Adding @Consumes(MediaType.APPLICATION_FORM_URLENCODED) to the Jersey service.
  • Wrapping the data object with JSON.stringify (I know I need to do that when contentType: 'json', but shouldn't have to here.

我知道我可以自己编写URL参数,并在URL中填入它们,但这似乎不太雅致,我不必这样做.

I know I can code the URL parameters myself and stuff 'em in the URL, but that seems inelegant and I shouldn't have to do that.

推荐答案

$.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        url: url,
        data: data,
        dataType: 'json',
        timeout: 3000,
        // jsonpCallback:"foo",
        success:function(response){
            console.log(response);              
        },
        error: function(){
            console.log("error!");
        }
    });

上面是一个jquery请求,就像@futuretelematics所说的那样,我们需要contentType:"application/x-www-form-urlencoded; charset = utf-8". 然后在Jersey服务中添加@Consumes({MediaType.APPLICATION_FORM_URLENCODED}).

Above is a jquery request and just as @futuretelematics said we need contentType: "application/x-www-form-urlencoded;charset=utf-8". And then add @Consumes( {MediaType.APPLICATION_FORM_URLENCODED}) in the Jersey service.

@POST
@Produces({ MediaType.APPLICATION_JSON})
@Consumes( {MediaType.APPLICATION_FORM_URLENCODED})
public String execute(@FormParam("stoYardName") String data,
                       @FormParam("domTree") String domTree
                                    ) {
            .........

};

这篇关于jQuery不能将URL参数发布到Jersey服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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