1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $('#update').click(function() { var doc = $.parseJSON('{"_id":"id","_rev":"rev","title":"HTML and CSS: Design' +'and Build Websites","price":100,"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 33 34 35 36 37 38 39 | @PUT @Produces(value = MediaType.) @Path(value = "/{db}/{id}") public Response SaveDocById(@PathParam("db") String db, @QueryParam("rev") String rev, JSONObject json) { String responseString = null; HttpPut httpPut = new HttpPut(cloudantURL + db); System..println(cloudantURL + db); CloseableHttpResponse response = null; CloseableHttpClient client = getClient(); try { StringEntity entity = new StringEntity(json.toString()); entity.setContentType("application/json"); httpPut.setEntity(entity); httpPut.setHeader("Connection", "keep-alive"); if(null != rev && !"".equals(rev)){ httpPut.setHeader("If-Match", rev); } response = client.execute(httpPut); HttpEntity ReEntity = response.getEntity(); 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(); } System..println(responseString); return Response.serverError().build();; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | $('#delete').click(function() { var doc =$.parseJSON('{"_id": id,"_rev":rev}'); $.couch.urlPrefix = '../jaxrs/cloudant'; var db = $.couch.db('crud'); db.removeDoc(doc, { success : function(response, textStatus, jqXHR) { console.log(response); }, error : function(jqXHR, textStatus, errorThrown) { // do something else if it goes wrong } }); }); |
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 | @DELETE @Produces(value = MediaType.APPLICATION_JSON) @Path(value = "/{db}/{docid}") public Response deleteDocById(@PathParam("db") String db, @PathParam("docid") String docid, @QueryParam("rev") String rev) { HttpDelete httpDelete = new HttpDelete(cloudantURL + db + "/" + docid); System.out.println(cloudantURL + db + "/" + docid); httpDelete.setHeader("X-SyncTimeOut", "300"); httpDelete.setHeader("If-Match", rev); HttpResponse response = null; CloseableHttpClient client = getClient(); try { response = client.execute(httpDelete); 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 (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |