这是我的服务 -
@GET
@Path("{id}/json")
@Produces("application/json")
public Response getImageByIdJson(@PathParam("id") int imageId) {
//return this.getInageById(imageId);
List<Image> imageList = null;
try {
imageList = intlxBusiness.getAllImage();
} catch (SQLException e) {
e.printStackTrace();
}
Image returnImage = null;
for (Image image : imageList) {
if (image.getImageId() == imageId)
returnImage = image;
}
// Creates a ResponseBuilder
ResponseBuilder builder = new ResponseBuilderImpl();
// Sets the header of the return data
builder.header("content-type", "application/json");
// Sets the response code as a 200
builder.status(Response.Status.OK);
// Allows Cross Domain Requests from any origin
builder.header("Access-Control-Allow-Origin", "*");
// Adds your result data to the Response
builder.entity(returnImage);
// Creates the Response
return builder.build();
}
这是我的ajax电话 -
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
type:"GET",
url: "http://localhost:8888/IntellixWebApi/intellixservices/activeimage/1/json",
headers : {Accept : "application/json","Access-Control-Allow-Origin" : "*"}
dataType:"json",
crossDomain: true,
success: function(data){
alert(data);
},
error: function(msg){
alert("ERROR! \n" + msg);
}
});
</script>
这是我的回复屏幕截图 -
但是没有数据。
当我通过浏览器打电话时;然后我得到以下回复 -
在Json
{"Image-Id":1,"Image-Name":"Visakha Star","Image-Url":"F:/Recieved File/vishaka.jpg","Image-Description":"Description for Star"}
在Xml中 -
<Images Image-Id="1" debug="true">
<script id="FirebugLite" firebugIgnore="true" extension="Chrome"/>
<Image-Name>Visakha Star</Image-Name>
<Image-Url>F:/Recieved File/vishaka.jpg</Image-Url>
<Image-Description>Description for Star</Image-Description>
</Images>
请告诉我哪里错了。该服务将用于CORS。我在服务方法中添加了所需的信息。
EDIT ---
如果我使用这个ajax代码 -
$.ajax({
type:"GET",
url: "XXXX",
cache: false,
async: false,
crossDomain: true,
jsonp: true,
jsonpCallback: "callback",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown){alert('fail by event '+errorThrown)}
});
然后我得到了回复,但看了下面的图片 -
响应xml是正确的。
由于 Kumar Shorav