2012-12-29 45 views
0

您好我正在開發一個使用REST Web服務作爲服務器端的Web應用程序。當我從客戶端撥打電話以休息Web服務時,它會給我以下錯誤。服務正在運行http://localhost:8010/service,客戶端正在運行http://localhost:8020/client。我已經在碼頭部署了這個。訪問控制 - 允許來源

XMLHttpRequest cannot load http://localhost:8010/Service/rest/employee/basicupdate. Origin http://localhost:8020 is not allowed by Access-Control-Allow-Origin. 

下面我已經包含在您使用的瀏覽器我的休息方法

@POST 
@Path("/basicupdate") 
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 
@Context 
public String isValidLogin(@FormParam("employeeId") int employeeId, @FormParam("firstName") String firstName, @FormParam("lastName") String lastName, @FormParam("gender") String gender, @FormParam("dob") String dob) throws JsonGenerationException, JsonMappingException, IOException{ 
    Response response = new Response(); 
    String responseString = ""; 
    try { 

     //Application logic 

     response.setCode(MessageCode.SUCCESS); 
     response.setMessage("Successfully Updated"); 

    } catch (CustomException e) { 
     response.setCode(MessageCode.ERROR); 
     response.setMessage(e.getMessage()); 
     e.printStackTrace(); 
    } catch (Exception e) { 
     response.setCode(MessageCode.ERROR); 
     response.setMessage(e.getMessage()); 
     e.printStackTrace(); 
    }finally{ 
     responseString = mapper.writeValueAsString(response); 
    } 
    return responseString; 
} 
+0

? –

回答

1
<web-app> 
<filter> 
    <filter-name>cross-origin</filter-name> 
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class> 
    <init-param> 
     <param-name>allowedOrigins</param-name> 
     <param-value>*</param-value> 
    </init-param> 
    <init-param> 
     <param-name>allowedMethods</param-name> 
     <param-value>*</param-value> 
    </init-param> 
    <init-param> 
     <param-name>allowedHeaders</param-name> 
     <param-value>*</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>cross-origin</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
</web-app> 
+0

thnx它解決了問題:) – Shanaka

+0

我有另一個問題。現在這工作正常。當我發佈請求時,Web服務會更新所需的數據並向我發送響應,而不會出現任何錯誤。響應狀態也是200.但是這個響應來的jquery方法的錯誤方法沒有成功。可能是什麼原因? – Shanaka

+0

你不提供任何關於你正在使用的REST框架的細節。 –