[英]call a java webservice using javascript
I created a java Restfull webservice using patterns in netbeans and run the project in one machine. 我使用netbeans中的模式创建了Java Restfull Web服务,并在一台计算机上运行该项目。 How can i call this web service from another machine in javascript
如何在JavaScript中从另一台计算机调用此Web服务
The webservice class is webservice类是
package com.gdb.webapi;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import static javax.ws.rs.HttpMethod.POST;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
/**
* REST Web Service
*
* @author suhail
*/
@Path("displaylist")
public class DisplaylistResource {
@Context
private UriInfo context;
/**
* Creates a new instance of DisplaylistResource
*/
public DisplaylistResource() {
}
/**
* Retrieves representation of an instance of
* com.gdb.appconstant.DisplaylistResource
*
* @return an instance of java.lang.String
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getJson() {
//TODO return proper representation object
throw new UnsupportedOperationException();
}
/**
* PUT method for updating or creating an instance of DisplaylistResource
*
* @param content representation for the resource
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String putJson(String content) throws ParseException {
System.out.println(content);
return "true";
}
}
I want to call the post method using ajax 我想使用ajax调用post方法
Create path of your service call. 创建服务呼叫的路径。 http://YOUR IP/PROJECT NAME/ACTION NAME.
http://您的 IP /项目名称/操作名称。
eg 例如
function fun()
{
var data="hello";
$.get("http://localhost/projectNAME/HelloWorld", function(response) {
data = response;
}).error(function(){
alert("Sorry could not proceed");
});
return data;
}
and also put @path annotation for methods, which ll decide which method is going to call. 并为方法添加@path注释,这将决定要调用的方法。
I would use the fetch polyfill -> https://github.com/github/fetch . 我将使用提取 polyfill-> https://github.com/github/fetch 。 Look at the readme to see how to post data to a REST service:
查看自述文件,了解如何将数据发布到REST服务:
fetch('/users', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Hubot',
login: 'hubot',
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.