1

Here is the scenario I have a REST service defined as follow:

@Path("/company/{companyName}/sessions")
public class RESTSessionController {

    RESTService service = new RESTService();
     @GET
     @Produces({"application/json"})
     @Path("/{username}/{password}") 
     public Result getFriend(@PathParam ("companyName") String companyName ,
             @PathParam ("username") String username,
             @PathParam ("password") String password){

         System.out.println(companyName);
         return service.login(username,password);
     }
}

To call this I have a javascript as follow:

$.ajax({
    url: 'http://localhost:8888/rest/company/hertz/sessions/amir/help',
    dataType: 'json',
    data: null,
    success: function(data) { 
        $("#abc").html(dumpObj(data,"Result",'',0));
         }
    });

This works fine and I get this back:

{"code":"200","description":"Amir is now logged in.","payload":{"@type":"xs:string","$":"Amir123"}}

which is perfectly fine.

Now I'm trying to go one step further and call my service using an object so I changed my code to:

$.ajax({
    qObj={username:"Amir",password:"123",companyName:"hertz"}
    url: 'http://localhost:8888/rest/company/',
    dataType: 'json',
    data: JSON.stringify(qObj),
    success: function(data) { 
        $("#abc").html(dumpObj(data,"Result",'',0));
         }
    });

And it won't work.

My question is simple, how to I call a REST service with parameter in the path (or without it) using jquery/javascript?

Thx for the help

Amir

3
  • I know this is off topic, but using a GET request to log a user in sounds like a really bad idea. Commented Aug 1, 2012 at 16:26
  • I think he is just playing around with jquery otherwise this is a very bad practice since password is not hashed on top of what you mentioned. Commented Aug 1, 2012 at 16:34
  • Jay, thx for the comment, Quynh is right, this is simply me trying to build my framework and make sure I have all I need before starting the main application. Your both comments are valid about using GET and plain password. Commented Aug 1, 2012 at 17:57

2 Answers 2

1

Found the solution In case you are looking for the same answer here are my findings: 1) You can't user variables used in the path as part of your objects sent to REST services. So in my case {companyName} couldn't be really be passed as part of json object. 2) In method declaration it has to be a directive to indicate this method expects a json object, this was the case so I changed my code to:

 @PUT
 @Produces(MediaType.APPLICATION_JSON)
 @Consumes(MediaType.APPLICATION_JSON)
 @Path("/")
 public API_Session login(API_Session_Request request){

and all worked like a charm.

3) In javascript you should use the JSON.stringify your object:

var qObj={username:"Amir",password:"123",company_name:"hertz"}
$.ajax({
    type:'PUT',
    url: 'http://localhost:8888/rest/company/sessions',
    dataType: 'json',
    data: JSON.stringify(qObj),
    contentType: "application/json; charset=utf-8",
    success: function(data) { 
        $("#abc").html(dumpObj(data,"Result",'',0));
         }
    });

Hope this helps you to save some time.

Sign up to request clarification or add additional context in comments.

Comments

0

why do you have to use JSON.stringify? Try this and let me know :)

$.ajax({
    url: 'http://localhost:8888/rest/company/',
    dataType: 'json',
    data: {username:"Amir",password:"123",companyName:"hertz"},
    success: function(data) { 
        $("#abc").html(dumpObj(data,"Result",'',0));
         }
    });

4 Comments

Tried this and the same result. It's getting 404 page not found result form Jersey.
I see 4 parameters here /hertz/sessions/amir/help but your data only contains 3.
Sessions is part of the path, it's not the parameter.
ok, try this @Path("/company/sessions/{companyName}") and change your url path inside the ajax call. So. all of your url should be 'localhost:8888/rest/company/sessions'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.