JERSEY-RS showing 405 in POST

给你一囗甜甜゛ 提交于 2019-12-24 18:09:04

问题


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.


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/21061332/jersey-rs-showing-405-in-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!