问题描述
我试过几个教程使用jQuery调用Ajax调用REST风格的Web服务,但我没有得到回应。但是当我直接打网址在浏览器中我得到了响应,但无法获得相同的JSON响应使用Ajax调用,总是会在错误块。(Tomcat是在8888端口上运行)
的http://本地主机:8888 / WebService_2 / REST / Web服务
index.jsp文件。
<%@页面语言=Java的的contentType =text / html的;字符集= UTF-8
的pageEncoding =UTF-8%>
!< DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01过渡// ENhttp://www.w3.org/TR/html4/loose.dtd">
< HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
<标题>此处插入标题< /标题>
< /头>
&所述;脚本的src =https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>&所述; /脚本>
<身体GT;
<表>
&其中; TR>
< TD><输入类型=按钮值=提交ID =提交> < / TD>
< / TR>
< /表>
< /身体GT;
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$(#提交)。点击(函数(){
$阿贾克斯({
键入:GET,
数据类型:JSON,
的contentType:应用/ JSON的;字符集= UTF-8,
网址:HTTP://本地主机:8888 / WebService_2 / REST / Web服务,
成功:函数(数据1){
的console.log(回应:+ DATA1);
},
错误:函数(jqXHR,textStatus,errorThrown){
的console.log('在处理错误!);
}
});
});
});
< / SCRIPT>
< / HTML>
WebSerivce类。
包com.app.rest;
进口javax.ws.rs.GET;
进口javax.ws.rs.Path;
进口javax.ws.rs.Produces;
进口javax.ws.rs.core.MediaType;
@Path(/ web服务)
公共类WebSerivce {
@得到
@Produces(MediaType.APPLICATION_JSON)
公共字符串的GetResponse(){
返回Web服务响应呼叫;
}
}
的web.xml
<的servlet>
< servlet的名称>新泽西州的Serlvet< / Servlet的名称>
<的servlet类> com.sun.jersey.spi.container.servlet.ServletContainer< / Servlet的类>
<初始化参数>
<参数 - 名称>&com.sun.jersey.config.property.packages LT; /参数 - 名称>
<参数值> com.app.rest< /参数值>
< /初始化参数>
&所述;负载上启动→1&其中; /负载上启动>
< / servlet的>
< Servlet映射>
< servlet的名称>新泽西州的Serlvet< / Servlet的名称>
< URL模式> / REST / *< / URL模式>
< / Servlet映射>
有一些谷歌后的答案...我已经使用了谷歌罐子JSON的转换
的pom.xml
<依赖>
<的groupId> com.google code.gson< /的groupId>
< artifactId的> GSON< / artifactId的>
<版> 2.2.4< /版本>
< /依赖性>
WebService.class
进口的java.util.ArrayList;
进口的java.util.List;
进口javax.ws.rs.GET;
进口javax.ws.rs.Path;
进口javax.ws.rs.Produces;
进口javax.ws.rs.core.MediaType;
进口com.google.gson.Gson;
进口com.google.gson.GsonBuilder;
@Path(/ web服务)
公共类WebSerivce {
@得到
@Produces(MediaType.APPLICATION_JSON)
公共对象的GetResponse(){
//名字,姓氏,年龄,身份证
学生STD1 =新的学生(益,测试,22.2);
学生STD2 =新的学生(约翰福音,矢量,23,3);
学生STD3 =新的学生(IKS,罗伊,25,4);
名单<学生> stuList =新的ArrayList<学生>();
stuList.add(STD1);
stuList.add(STD2);
stuList.add(STD3);
GSON GSON =新GsonBuilder()创建()。
返回gson.toJson(stuList);
}
}
index.jsp的
<%@页面语言=Java的的contentType =text / html的;字符集= UTF-8
的pageEncoding =UTF-8%>
!< DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01过渡// ENhttp://www.w3.org/TR/html4/loose.dtd">
< HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
<标题>此处插入标题< /标题>
< /头>
&所述;脚本的src =https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>&所述; /脚本>
<身体GT;
<形式方法=获得>
<表>
&其中; TR>
< TD><输入类型=按钮值=提交ID =提交> < / TD>
< DIV ID =数据>< / DIV>
< / TR>
< /表>
< /形式GT;
< /身体GT;
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
VAR VAL =;
$(#提交)。点击(函数(事件){
。事件preventDefault();
$阿贾克斯({
键入:GET,
数据类型:JSON,
网址:休息/ web服务,
成功:功能(数据){
的console.log(回应:+数据);
$每个(数据,功能(J,PDATA){
VAL = VAL +[+ pdata.firstName ++ pdata.lastName +];
});
$(#数据)文本(VAL)。
},
错误:函数(jqXHR,textStatus,errorThrown){
的console.log('错误的处理!'+ textStatus);
}
});
});
});
< / SCRIPT>
< / HTML>
I tried couple of tutorial to call rest web-service using jQuery ajax call, but I am not getting the response.but When I directly hitting the url in browser I am getting the response ,but unable to get same json response using ajax call,always going in the error block.(tomcat is running on port 8888)
http://localhost:8888/WebService_2/rest/webservice
Index.jsp file.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<body>
<table>
<tr>
<td><input type="button" value="submit" id="submit"> </td>
</tr>
</table>
</body>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
$.ajax({
type: "GET",
dataType:"json",
contentType: "application/json; charset=utf-8",
url: "http://localhost:8888/WebService_2/rest/webservice",
success: function(data1) {
console.log("response:" + data1);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(' Error in processing!');
}
});
});
});
</script>
</html>
WebSerivce Class.
package com.app.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/webservice")
public class WebSerivce {
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getResponse(){
return "Web Service Response Call " ;
}
}
web.xml
<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.app.rest</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>
Got the answer after some google...I have used the google jar for JSON conversion
Pom.xml
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
WebService.class
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@Path("/webservice")
public class WebSerivce {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Object getResponse(){
//firstName,LastName,Age,Id
Student std1 = new Student("ik","test",22,2);
Student std2 = new Student("John","Vector",23,3);
Student std3 = new Student("iks","Roy",25,4);
List<Student> stuList = new ArrayList<Student>();
stuList.add(std1);
stuList.add(std2);
stuList.add(std3);
Gson gson = new GsonBuilder().create();
return gson.toJson(stuList);
}
}
Index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<body>
<form method="get">
<table>
<tr>
<td><input type="button" value="submit" id="submit"> </td>
<div id="data"></div>
</tr>
</table>
</form>
</body>
<script type="text/javascript">
$(document).ready(function(){
var val = "";
$("#submit").click(function(event){
event.preventDefault();
$.ajax({
type: "GET",
dataType:"json",
url: "rest/webservice",
success: function(data) {
console.log("response:" + data);
$.each(data, function(j, pdata) {
val= val + "[ "+pdata.firstName +" " + pdata.lastName +"]";
});
$("#data").text(val);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(' Error in processing! '+textStatus);
}
});
});
});
</script>
</html>
这篇关于调用使用jQuery AJAX休息web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!