我試圖建立一個REST滿Web服務。REST滿Web服務與澤西和jQuery得到奇怪的響應
在前端我使用jQuery來獲取數據。
在後端我從www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/
調用127.0.0.1:8080/restEx/的例子嘗試它REST/JSON /從瀏覽器中我得到的答案獲得預期:
{"title":"Enter Sandman","singer":"Metallica"}
但是,當我嘗試$。獲得()來獲取這些數據,我沒有得到任何答覆。 看着Firebug告訴我,我沒有收到數據。
我也
return Response.ok("{status: 'success'}").header("Access-Control-Allow-Origin", "*").build();
試了一下,因爲我發現在另一個問題 在螢火蟲的解決方案,現在我看到的數據,但仍然jQuery的沒有執行成功的功能。我仍然只看到失敗功能。
那麼我做錯了什麼?我怎樣才能使這個東西工作?
我正在使用jQuery 2.0.2,Jersey 1.8(我使用Jersey 2從jersey.java.net/documentation/latest/getting-started.html#running-project嘗試了它,結果相同)並且部署了項目在Tomcat 7
的index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="assets/js/jquery-2.0.2.js"></script>
</head>
<body>
<div id="first" class="container"></div>
<script type="text/javascript">
var url = "http://127.0.0.1:8080/restEx/rest/json/get";
$.get(url, {}, function(){alert("success");},"json")
.fail(function(){alert("fail");})
;
</script>
</body>
</html>
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>RESTfulExample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>RESTfulExample Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<finalName>restEx</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
的web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mkyong.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
JSONService.java
package com.mkyong.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.mkyong.Track;
@Path("/json")
public class JSONService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON() {
Track track = new Track();
track.setTitle("Enter Sandman");
track.setSinger("Metallica");
return track;
}
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response createTrackInJSON(Track track) {
String result = "Track saved : " + track;
return Response.status(201).entity(result).build();
}
@GET @Path("/test") @Produces(MediaType.APPLICATION_JSON)
public JSONObject test() throws JSONException{
return new JSONObject("{status:'success'}");
}
}
Track.java
package com.mkyong;
public class Track {
String title;
String singer;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
@Override
public String toString() {
return "Track [title=" + title + ", singer=" + singer + "]";
}
}
有了正確的URL的行爲發生:( 我試着用這個servlet也與相同的行爲: 公共類JSONServlet延伸的HttpServlet { \t公共無效的doGet(HttpServletRequest的請求,HttpServletResponse的響應) \t \t response.setContentType( 「應用程序/ JSON」); \t \t PrintWriter的OUT = response.getWriter(); \t \t通過out.println(arrayObj); \t \t out.close(); \t} } –
現在你有一個交叉請求問題。也許會工作,如果你嘗試這個技巧:[鏈接](http://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin-resource-sharing-cors-post-request-working)。 –
謝謝你的提示。有了這些信息,我可以解決我的問題。我也發現這[鏈接](http://blog.usul.org/cors-compliant-rest-api-with-jersey-and-containerresponsefilter/)。 –