怎样使用ajax调用restful webservice (传json)??
我先创建一个pojo
@XmlRootElementpublic class Category {private int id;private String name;//....} 然后创建restful webservice resource类
@PUT@Path("add")@Produces("text/html")@Consumes( { "application/xml", "application/json" })public String addCategory(Category category) {System.out.println("处理添加类别逻辑,接受的数据为id:"+category.getId()+",name:"+category.getName());return "ok";} 测试通过
http://www.agoit.com/upload/attachment/72697/d0df1879-6fc0-3c50-b260-b8bb8a204aa3.bmp
用ajax 调用时出 了问题(我用的是jquery)
$.ajax({url:"/restblog/services/category/add",type:"PUT",data:{"id":1,"name":"id为1的category"},success :function (result){alert(result);},error:function (){alert("add category failed!");}}); 我试过用
data:"{\"id\":1,\"name\",\"id为1的category\"}",报同样错误,错误在firebug下截图是这样的
http://www.agoit.com/upload/attachment/72703/3026db27-09ce-34b7-b43b-64a49d51393f.bmp
http://www.agoit.com/upload/attachment/72705/d8fe7989-4997-3b23-a450-a972879792eb.bmp
很让我不解。还请各位高手指教。
页:
[1]