I created a java Restfull webservice using patterns in netbeans and run the project in one machine. How can i call this web service from another machine in javascript
The webservice class is
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
Create path of your service call. http://YOUR IP/PROJECT NAME/ACTION NAME.
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.
I would use the fetch polyfill -> https://github.com/github/fetch . Look at the readme to see how to post data to a REST service:
fetch('/users', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Hubot',
login: 'hubot',
})
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.