Pergunta

My Service (RESTful JAX-RS) is receiving values like the following.

public Response methodName(String content,
        @QueryParam("valueONE") String one,
        @QueryParam("valueTWO") String two,
        @QueryParam("valueTHREE") String three)

From the Ajax call I have to pass values to that service. To my scenario, I have tried like the below. It is not working to me.

try {
    new Ajax.Request(url, {
        method : 'post',
        postBody : {
            Object.toJSON(content),
            valueONE : one,
            valueTWO : two,
            valueTHREE : three,
        },
        onSuccess : function(response) {
            alert('Success Response' + response.responseText);
        },
        onFailure : function(response) {
            alert('Request has not been hit to the server.'
                    + response.responseText);
        }
    });
} catch (err) {txt = "_____ERROR_____\n\n";
        txt += "ERROR DESCRIPTION : " + err.message + "\n\n";
        txt +=" Click OK to continue."; 
        alert(txt);
}

I dont know how to pass values via body as well as queryparameter in Prototype.js. Any help is much appreciated.

Regards, ArunRaj

Foi útil?

Solução

When your request method is post, any parameters you set in the request will be sent in the body of the request. It is possible to also manually build in some querystring parameters in the URL you send to, although this is strictly not a correct way to send a POST (i.e. with a sidecar GET contained within it).

new Ajax.Request('path/to/server', {
  parameters: {
    foo: 'One',
    bar: 'Two',
    baz: 'Three'
  }
});

That's going to result in a set of POST variables [foo, bar, baz] being populated in the body of the request.

new Ajax.Request('path/to/server?boo=Four&blarg=Five', {
  parameters: {
    foo: 'One',
    bar: 'Two',
    baz: 'Three'
  }
});

That may, depending on the laxity of your server, result in the same POST along with a set of GET variables [boo, blarg] being populated by the querystring. On a PHP server, these are collated into the REQUEST global object, with same-name GET variables being overwritten by their POST equivalents, so you don't have to interrogate both sets of form data separately.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top