2014-10-01 99 views
0

我的問題看起來像這樣:當我發送POST請求到REST服務時,我得到Access-Control-Allow-Origin錯誤,但是請求GET工作。發送請求到REST服務時POST方法不起作用

這是我的休息服務:

@Path("/createUser") 
@RequestScoped 
public class ClientRestService { 

@Inject 
private ClientManager clientManager; 

@POST 
@Path("{name}/{surname}/{adress}") 
@Produces(MediaType.APPLICATION_JSON) 
public Response createUser(@PathParam("name")String name, @PathParam("surname")String surname, @PathParam("adress")String adress) { 
    Client client = new Client(name,surname,adress); 


    Response.ResponseBuilder builder = Response.ok("POST It's working.!!!"); 
    builder.header("Access-Control-Allow-Origin", "*"); 
    builder.header("Access-Control-Allow-Methods", "POST"); 
    return builder.build(); 
} 

@GET 
public Response getMetod() { 
    Response.ResponseBuilder builder = Response.ok("GET It's working."); 
    builder.header("Access-Control-Allow-Origin", "*"); 
    return builder.build(); 
} 
} 

這是客戶端:

$(document).ready(function() { 

$.ajax({ 
    url: 'http://localhost:8080/Bank_Project_Test/MyApp/createUser', 
    type: 'POST', 
    data: 'name=SomeName'+ 
     '&surname=SomeSurname'+ 
     '&adress=SomeAdress', 
    success: function(success) { 
     alert(success); 
    } 
}); 
}); 
+0

你有沒有想過這個?我在這裏有一個類似的問題:http://stackoverflow.com/questions/43690811/does-responsebuilder-work-with-get-request-but-not-post – SuperCow 2017-04-29 03:43:11

回答