Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make compatible with jQuery 1.5.x

git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@1067420 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 064a1d9483cfa33bd0fcc97cb1d97e8e342fdeaf 1 parent 4dea2df
Jason Davies authored February 05, 2011

Showing 1 changed file with 27 additions and 6 deletions. Show diff stats Hide diff stats

  1. 33  share/www/script/jquery.couch.js
33  share/www/script/jquery.couch.js
@@ -88,7 +88,7 @@
88 88
       $.ajax({
89 89
         type: "GET", url: this.urlPrefix + "/_session",
90 90
         complete: function(req) {
91  
-          var resp = $.httpData(req, "json");
  91
+          var resp = httpData(req, "json");
92 92
           if (req.status == 200) {
93 93
             if (options.success) options.success(resp);
94 94
           } else if (options.error) {
@@ -124,7 +124,7 @@
124 124
         type: "POST", url: this.urlPrefix + "/_session", dataType: "json",
125 125
         data: {name: options.name, password: options.password},
126 126
         complete: function(req) {
127  
-          var resp = $.httpData(req, "json");
  127
+          var resp = httpData(req, "json");
128 128
           if (req.status == 200) {
129 129
             if (options.success) options.success(resp);
130 130
           } else if (options.error) {
@@ -141,7 +141,7 @@
141 141
         type: "DELETE", url: this.urlPrefix + "/_session", dataType: "json",
142 142
         username : "_", password : "_",
143 143
         complete: function(req) {
144  
-          var resp = $.httpData(req, "json");
  144
+          var resp = httpData(req, "json");
145 145
           if (req.status == 200) {
146 146
             if (options.success) options.success(resp);
147 147
           } else if (options.error) {
@@ -385,7 +385,7 @@
385 385
             dataType: "json", data: toJSON(doc),
386 386
             beforeSend : beforeSend,
387 387
             complete: function(req) {
388  
-              var resp = $.httpData(req, "json");
  388
+              var resp = httpData(req, "json");
389 389
               if (req.status == 200 || req.status == 201 || req.status == 202) {
390 390
                 doc._id = resp.id;
391 391
                 doc._rev = resp.rev;
@@ -450,7 +450,7 @@
450 450
         copyDoc: function(docId, options, ajaxOptions) {
451 451
           ajaxOptions = $.extend(ajaxOptions, {
452 452
             complete: function(req) {
453  
-              var resp = $.httpData(req, "json");
  453
+              var resp = httpData(req, "json");
454 454
               if (req.status == 201) {
455 455
                 if (options.success) options.success(resp);
456 456
               } else if (options.error) {
@@ -593,6 +593,27 @@
593 593
     }
594 594
   });
595 595
 
  596
+  var httpData = $.httpData || function( xhr, type, s ) { // lifted from jq1.4.4
  597
+    var ct = xhr.getResponseHeader("content-type") || "",
  598
+      xml = type === "xml" || !type && ct.indexOf("xml") >= 0,
  599
+      data = xml ? xhr.responseXML : xhr.responseText;
  600
+
  601
+    if ( xml && data.documentElement.nodeName === "parsererror" ) {
  602
+      $.error( "parsererror" );
  603
+    }
  604
+    if ( s && s.dataFilter ) {
  605
+      data = s.dataFilter( data, type );
  606
+    }
  607
+    if ( typeof data === "string" ) {
  608
+      if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
  609
+        data = $.parseJSON( data );
  610
+      } else if ( type === "script" || !type && ct.indexOf("javascript") >= 0 ) {
  611
+        $.globalEval( data );
  612
+      }
  613
+    }
  614
+    return data;
  615
+  };
  616
+
596 617
   function ajax(obj, options, errorMessage, ajaxOptions) {
597 618
     options = $.extend({successStatus: 200}, options);
598 619
     ajaxOptions = $.extend({contentType: "application/json"}, ajaxOptions);
@@ -608,7 +629,7 @@
608 629
       },
609 630
       complete: function(req) {
610 631
         try {
611  
-          var resp = $.httpData(req, "json");
  632
+          var resp = httpData(req, "json");
612 633
         } catch(e) {
613 634
           if (options.error) {
614 635
             options.error(req.status, req, e);

2 notes on commit 064a1d9

Jens Tegelaers

Hi! Since the response text is already there, why not parse it into a JSON object?

  • var resp = $.httpData(req, "json");
  • var resp = JSON.parse(req.responseText);

json2.js is already provided in the project.

Jason Davies

Hi, thanks for the suggestion. This does indeed make more sense; I would prefer to use $.parseJSON (which attempts to use JSON.parse internally) to reduce explicit dependencies on json2.js (I know we already need it for toJSON, but jQuery is the main dependency). Implemented in 91543c0.

Please sign in to comment.
Something went wrong with that request. Please try again.