为什么我在做POST请求时获得405方法不允许 [英] Why am i getting 405 Method Not Allowed while doing a POST Request

查看:8740
本文介绍了为什么我在做POST请求时获得405方法不允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是休息服务的新人。我试图创建一个服务,从客户端接受json字符串。我得到405错误,当我使用JQuery调用此服务。下面是ws的Java代码:

I am new to the rest services. I am trying to create a service that accepts json string from a client. I am getting 405 error when I am calling this service using JQuery. Below is the Java code for ws:

这是我向JERSEY POST RESTFUL Webservice发送请求的方式。

This is the way i am posting a request to JERSEY POST RESTFUL Webservice .

var orderinfo = {'ordersplitjson': ordersplitjson, 'customer_id': cust_id , 'homedelivery': homedelivery, 'seatnum' :seatnum , 'locationname':location_nam , 'rownum':rownum}; 
var json_data =  JSON.stringify(orderinfo);
        var ajaxcallquery = $.ajax({
            type:'POST',
              dataType: 'jsonp',
             data: json_data,
             contentType: "application/json; charset=utf-8",
            url:url+'/OMS/oms1/orderinsertservice',
            jsonpCallback:'jsonCallback',
            jsonp:false,
            success: function(response) 
            {
            },
            error: function(jqxhr, status, errorMsg) {
            alert('Failed! ' + errorMsg);
        }

        });


 public class OrdersInsertService 
{
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String getData(OrderInfo order,@Context HttpServletResponse serverResponse)
    throws JSONException
            {
         serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
            serverResponse.addHeader("Access-Control-Allow-Credentials", "true");
            serverResponse.addHeader("Access-Control-Allow-Origin", "*");
           serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
            serverResponse.addHeader("Access-Control-Max-Age", "60");

}
}


package com.util;

public class OrderInfo {

    String ordersplitjson;
    public String getOrdersplitjson() {
        return ordersplitjson;
    }
    public void setOrdersplitjson(String ordersplitjson) {
        this.ordersplitjson = ordersplitjson;
    }
    public String getCustomer_id() {
        return customer_id;
    }
    public void setCustomer_id(String customer_id) {
        this.customer_id = customer_id;
    }
    public String getHomedelivery() {
        return homedelivery;
    }
    public void setHomedelivery(String homedelivery) {
        this.homedelivery = homedelivery;
    }
    public String getSeatnum() {
        return seatnum;
    }
    public void setSeatnum(String seatnum) {
        this.seatnum = seatnum;
    }
    public String getLocationname() {
        return locationname;
    }
    public void setLocationname(String locationname) {
        this.locationname = locationname;
    }
    public String getRownum() {
        return rownum;
    }
    public void setRownum(String rownum) {
        this.rownum = rownum;
    }
    String customer_id;

    String homedelivery;
    String seatnum;

    String locationname;
    String rownum;


}

任何人都请让我知道如何修复此问题

Could anybody please let me know how to fix this

>

我使用Jersey 1,当我使用你的类,它给我一个编译错误在eclipse作为hwon的图片

I am using Jersey 1 ,When i used your class its giving me a compiltion error in eclipse as hwon in picture

推荐答案

(1) dataType:'jsonp'应为 json 。摆脱所有的 jsonp 相关的东西。它应该是json。 jsonp会自动更改为GET请求,因此405(方法不允许)。您的资源方法接受POST。

(1) dataType: 'jsonp' should be json. Get rid of all the the jsonp related stuff. It should be json. jsonp will get automatically changed to a GET request, hence the 405 (method not allowed). Your resource method accepts POST.

(2)CORS问题。你需要一个过滤器来处理飞行前。如果你使用Jersey 1,你可以使用这个类

(2) Problem with CORS. You need a filter to handle the pre-flight. If you are using Jersey 1, you can use this class

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

public class CORSFilter implements ContainerResponseFilter {
    @Override
    public ContainerResponse filter(ContainerRequest request,
            ContainerResponse response) {

        response.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
        response.getHttpHeaders().add("Access-Control-Allow-Headers",
                "origin, content-type, accept, authorization");
        response.getHttpHeaders().add("Access-Control-Allow-Credentials", "true");
        response.getHttpHeaders().add("Access-Control-Allow-Methods",
                "GET, POST, PUT, DELETE, OPTIONS, HEAD");

        return response;
    }
}

然后注册为

resourceConfig.getContainerResponseFilters().add(new CORSFilter());

使用web.xml,添加

With web.xml, add this

<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
  <param-value>com.yourpackage.CORSFilter</param-value>
</init-param>

包含Jersey servlet

inside the <servlet> element that contains the Jersey servlet

这篇关于为什么我在做POST请求时获得405方法不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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