1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $('#create').click(function() { var doc = $.parseJSON('{"title":"HTML and CSS: Design and Build Websites",' +'"price":20,"authors":["Jon Duckett"],"language":"html"}'); $.couch.urlPrefix = '../jaxrs/cloudant'; var db = $.couch.db('crud'); db.saveDoc(doc,{ success : function(response, textStatus, jqXHR) { console.log('success'); // do something if the save works }, error : function(jqXHR, textStatus, errorThrown) { // do something else if it goes wrong console.log('error'); } }); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @POST @Produces(value = MediaType.) @Path(value = "/{db}") public Response SaveDoc(@PathParam("db") String db, JSONObject json) { HttpPost httpPost = new HttpPost(cloudantURL + db); System..println(cloudantURL + db); CloseableHttpResponse response = null; CloseableHttpClient client = getClient(); try { StringEntity entity = new StringEntity(json.toString()); entity.setContentType("application/json"); httpPost.setEntity(entity); httpPost.setHeader("Connection", "keep-alive"); response = client.execute(httpPost); HttpEntity ReEntity = response.getEntity(); String responseString = EntityUtils.toString(ReEntity, "UTF-8"); int status = response.getStatusLine().getStatusCode(); Header[] headers = response.getAllHeaders(); client.close(); return httpResponse2JsonRespone(status, responseString, headers); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Response.serverError().build(); } |
1 2 3 4 5 6 7 8 9 10 | $('#read').click(function() { $.couch.urlPrefix = '../jaxrs/cloudant'; var db = $.couch.db('crud'); db.openDoc(id,{ success:function(data){ $('#read').innerhtml console.log(data); } }); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | @GET @Produces(value = MediaType.APPLICATION_JSON) @Path(value = "/{db}/{docid}") public Response getDocById(@PathParam("db") String db, @PathParam("docid") String docid) { HttpGet httpGet = new HttpGet(cloudantURL + db + "/" + docid); System.out.println(cloudantURL + db + "/" + docid); HttpResponse response = null; CloseableHttpClient client = getClient(); try { response = client.execute(httpGet); HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); System.out.println(responseString); int status = response.getStatusLine().getStatusCode(); Header[] headers = response.getAllHeaders(); client.close(); return httpResponse2JsonRespone(status, responseString, headers); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Response.serverError().build(); } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |