JERSEY-RS在POST中显示405 [英] JERSEY-RS showing 405 in POST

查看:96
本文介绍了JERSEY-RS在POST中显示405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jersey创建发布Web服务.以下是我的PostMethod;

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>";
}

以下是我的index.jsp代码.

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");}
 });

}

当我以表格形式输入数据并点击调用时,它会给我HTTP状态405-不允许使用方法错误.我已经在单击调用时对其进行了调试,它转到post方法并在返回时给出错误.

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.

推荐答案

我认为问题出在这里:

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

如果您的AJAX调用成功返回,这将导致浏览器导航到与AJAX请求提交到的URL相同. (我相信)这将导致该URL的GET,如果该URL仅处理POST,您将收到HTTP 405 Method Not Allowed错误.

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.

您发送回浏览器的响应对象中包含一个URL.相反,您需要从响应对象中获取URL并导航至该对象.

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.

这篇关于JERSEY-RS在POST中显示405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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