我是新手对于rest服务。我正在尝试创建一个服务,该服务接受来自客户端的JSON字符串。当我使用JQuery调用此服务时,我会收到405错误。 以下是ws的Java代码:
@POST
@Path("logevent")
@Consumes(MediaType.APPLICATION_JSON)
public boolean logEvent(String obj)
{
System.out.println(obj);
return true;
}
并且
@Path("getdata")
@GET
public String getData()
{
return "Hello";
}
发送JSON的jQuery代码如下:
var json ="{\"userName\":\"testtest\"}";
var json_data = JSON.stringify(json);
$.ajax({
type: "POST",
url: "http://localhost:8080/log/log/logevent",
// The key needs to match your method's input parameter (case-sensitive).
data: json_data,
contentType: "application/json",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
出了什么问题?发布(post)不起作用,但是当我使用URL http://<serverip>/log/log/getdata
进行get请求时,我可以得到响应。
http://localhost:8080/
,在浏览器中使用<serverip>
。这两者是相同的吗? - user1907906