1

I am trying to create a post webservice using jersey. Following is my PostMethod;

@POST
@Path("getEncodedURL")
@Produces(MediaType.TEXT_XML)
public String getEncodedURL(@FormParam(value = "url") String url, @FormParam(value = "eventId") int eventId) {
//mylogic here to get encoded url.
return "<data><eventId>"+eventId+"</eventId><url>"+url+"</url></data>";
}

Following is my index.jsp code.

<form id="form10" method="post">
        Enter URL to Encode: (String) <input type="text" name="testName" id="testName">
        Event id: (int) <input type="text" name="testEventId" id="testEventId">
        <input type="button" Value="Invoke Web Service" onclick="getEncodedURLAgainstURL();">
    </form>

function getEncodedURLAgainstURL(){
var testName = $("#testName").val();
var testEventId = $("#testEventId").val();


url = loc+"services/SnapEvent/getEncodedURL";
$.ajax({
    type: "post",
    url: url,
    data:{'eventId': testEventId, 'url': testName},
   /* dataType:"text",
    contentType: "text/html",*/
     success: function(resp){window.location = url},
     error: function(e){ alert("An error has occured");}
 });

}

when i enter data in form and hit invoke it gives me HTTP Status 405 - Method Not Allowed error. i have debugged it on clicking invoke it goes to the post method and gives error on return.

S0haib Nasir
  • 232
  • 2
  • 12
  • Interesting. Have you tried to validate the same request with curl? – Peter Jan 11 '14 at 11:34
  • like so: curl -H -X POST --data 'eventId=value1&url=value' http ://localhost:8080/services/SnapEvent/getEncodedURL – Peter Jan 11 '14 at 11:37
  • Sorry for the blank between http and ://, don't know how to escape urls here. – Peter Jan 11 '14 at 11:38
  • I dont know about curl. Can you explain its full syntax here please? – S0haib Nasir Jan 11 '14 at 12:09
  • curl is a command line tool for issuing / sending different requests. I use it often as a quick testing tool for webservices. E.g.:curl -H "Content-Type: application/json" -X POST --data '[{"name":"Jersey","site":"http://jersey.java.net"},{"age":33,"phone":"158158158","name":"Foo"},{"name":"JSON-P","site":"http://jsonp.java.net"}]' http://localhost:8080/jsonp-webapp/document/multiple for testing the jersey-examples-json-processing-webapp . Because as you can see in this example, the tool is rather complete so is it's syntax. See http://curl.haxx.se/docs/ for full documentation. – Peter Jan 11 '14 at 12:14
  • http://curl.haxx.se/docs/httpscripting.html#POST is the one related to HTTP / POST, as you can see it's basically curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi – Peter Jan 11 '14 at 12:17
  • thnx peter i will look into it and will get back to you. – S0haib Nasir Jan 11 '14 at 12:33

2 Answers2

0

when you use ajax to sent the parameters, they are @QueryParam , not the @FormParam, then the html can be changed like this.

<form id="form10" method="post">
    Enter URL to Encode: (String) <input type="text" name="url" id="testName">
    Event id: (int) <input type="text" name="eventId" id="testEventId">
    <input type="submit" Value="Invoke Web Service" />
</form>
StackBox
  • 326
  • 1
  • 6
  • Query param is used if the param are in url like http://localhost:8080/snap/getencodedurl?eventid=1&url=http...etc. I tried that as well but still same error...:( – S0haib Nasir Jan 11 '14 at 12:13
0

I think the problem is here:

 success: function(resp){window.location = url},

If your AJAX call returns successfully, this causes the browser to navigate to the same URL the AJAX request was submitted to. This will (I believe) result in a GET to to this URL, and if the URL only handles POSTs you will get an HTTP 405 Method Not Allowed error.

The response object you send back to the browser contains a URL inside it. What you need to do instead is to get the URL out of the response object and navigate to that instead.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • url is a variable of address on which it changed the window location. i changed the url to google.com and it transferred without any issue. i believe there is some issue in return type. i am returning a string of customized xml and it is not displaying it. – S0haib Nasir Jan 11 '14 at 12:40
  • @StrugglingUser: if you set `window.location = 'google.com'` in your `success` handler, then yes, that redirect should work. Your web-service is quite probably successfully returning XML to the browser, and I would expect your `success` handler to be called with it, in the `resp` parameter. However, your `success` handler does nothing with `resp`, and that's why your XML string isn't being displayed. – Luke Woodward Jan 11 '14 at 13:06
  • Yes your are right @Luke. what i was doing here was hitting that link through post method then changing the location again to that link which cause him to search for get method. can you guide me here that how can i change the url of page without hitting again. – S0haib Nasir Jan 11 '14 at 13:52
  • @StrugglingUser: here's a question that might help you get data out of an XML AJAX request: http://stackoverflow.com/questions/6052092/ – Luke Woodward Jan 11 '14 at 14:08