2014-03-06 102 views
0

下午!我對JavaScript和DocuSign API非常陌生。我試着按照示例,但我得到一個ReferenceError。DocuSign API - JavaScript

出於測試目的,我有async.js,fs.js,request.js和require.js在文件夾的根在我的電腦上測試HTML文檔

HTML一起:

<html> 
<head> 
     <script src="require.js"></script> 
     <script src="example.js"></script> 

</head> 

<body> 
<div id="message"></div> 
</body> 

</html> 

我收到以下錯誤: 的ReferenceError:異步沒有定義 async.waterfall(

下面是example.js的代碼,我從網站的DocuSign了,slighlt地調節基於另一個錯誤周圍的需要功能:

// Request Signature on a Document (JavaScript) 

// To run this sample 
// 1. Copy the file to your local machine and give .js extension (i.e. example.js) 
// 2. Change "***" to appropriate values 
// 3. Install async and request packages 
//  npm install async 
//  npm install request 
//  npm install fs 
// 4. execute 
//  node example.js 
// 

//var  async = require("async"),  // async module 
//  request = require("request"),  // request module 
//  fs = require("fs");   // fs module 

require(["request"], function (request){}); 
require(["async"], function (async){}); 
require(["fs"], function (fs){}); 


var  email = "[email protected]",    // your account email 
     password = "apassword",   // your account password 
     integratorKey = "akey",   // your Integrator Key 

(found on the Preferences -> API page) 
    recipientName = "Bob",   // recipient (signer) name 
     documentName = "afile",   // copy document with this name into same 

directory! 
     baseUrl = "";    // we will retrieve this through the Login call 

async.waterfall(
    [ 
    ///////////////////////////////////////////////////////////////////////////////////// 
    // Step 1: Login (used to retrieve your accountId and baseUrl) 
    ///////////////////////////////////////////////////////////////////////////////////// 
function(next) { 
     var url = "https://demo.docusign.net/restapi/v2/login_information"; 
     var body = ""; // no request body for login api call 

     // set request url, method, body, and headers 
     var options = initializeRequest(url, "GET", body, email, password); 

     // send the request... 
     request(options, function(err, res, body) { 
      if(!parseResponseBody(err, res, body)) { 
       return; 
      } 
      baseUrl = JSON.parse(body).loginAccounts[0].baseUrl; 
      next(null); // call next function 
     }); 
    }, 

    ///////////////////////////////////////////////////////////////////////////////////// 
    // Step 2: Request Signature on a PDF Document 
    ///////////////////////////////////////////////////////////////////////////////////// 
    function(next) {  
     var url = baseUrl + "/envelopes"; 
     // following request body will place 1 signature tab 100 pixels to the right and 
     // 100 pixels down from the top left of the document that you send in the request 
     var body = { 
      "recipients": { 
       "signers": [{ 
        "email": email, 
        "name": recipientName, 
        "recipientId": 1, 
        "tabs": { 
         "signHereTabs": [{ 
          "xPosition": "100", 
          "yPosition": "100", 
          "documentId": "1", 
          "pageNumber": "1"      


         }] 
        } 
       }] 
      }, 
      "emailSubject": 'DocuSign API - Signature Request on Document Call', 
      "documents": [{ 
       "name": documentName, 
       "documentId": 1, 
      }], 
      "status": "sent", 
     }; 

     // set request url, method, body, and headers 
     var options = initializeRequest(url, "POST", body, email, password); 

     // change default Content-Type header from "application/json" to "multipart/form-data" 
     options.headers["Content-Type"] = "multipart/form-data"; 

     // configure a multipart http request with JSON body and document bytes 
     options.multipart = [{ 
        "Content-Type": "application/json", 
        "Content-Disposition": "form-data", 
        "body": JSON.stringify(body), 
       }, { 
        "Content-Type": "application/pdf", 
        'Content-Disposition': 'file; filename="' + documentName + '"; 

documentId=1', 
        "body": fs.readFileSync(documentName), 
       } 
     ]; 

     // send the request... 
     request(options, function(err, res, body) { 
      parseResponseBody(err, res, body); 
     }); 
    } // end function 
]); 

//*********************************************************************************************** 
// --- HELPER FUNCTIONS --- 
//*********************************************************************************************** 
function initializeRequest(url, method, body, email, password) {  
    var options = { 
     "method": method, 
     "uri": url, 
     "body": body, 
     "headers": {} 
    }; 
    addRequestHeaders(options, email, password); 
    return options; 
} 

/////////////////////////////////////////////////////////////////////////////////////////////// 
function addRequestHeaders(options, email, password) { 
    // JSON formatted authentication header (XML format allowed as well) 
    dsAuthHeader = JSON.stringify({ 
     "Username": email, 
     "Password": password, 
     "IntegratorKey": integratorKey // global 
    }); 
    // DocuSign authorization header 
    options.headers["X-DocuSign-Authentication"] = dsAuthHeader; 
} 

/////////////////////////////////////////////////////////////////////////////////////////////// 
function parseResponseBody(err, res, body) { 
    console.log("\r\nAPI Call Result: \r\n", JSON.parse(body)); 
    if(res.statusCode != 200 && res.statusCode != 201) { // success statuses 
     console.log("Error calling webservice, status is: ", res.statusCode); 
     console.log("\r\n", err); 
     return false; 
    } 
    return true; 
} 

再一次,我對此很新,所以任何幫助將非常感激,提前致謝!

更新 - 在意識到我無法在QuickBase代碼頁上使用多個node.js,我嘗試了下面兩種編碼選項,既沒有工作......任何想法我做錯了什麼?

var xhr = createCORSRequest('GET', urlLoginCall); 
xhr.withCredentials = true; 
xhr.setRequestHeader('X-DocuSign-Authentication', jsonLoginCall); 
xhr.send(); 
xhr.onload = function() { 
    var responseText = xhr.responseText; 
    alert(responseText); 
}; 
xhr.onerror = function() { 
    alert('There was an error!'); 
}; 

$.ajax({ 
    type: 'GET', 
    url: urlLoginCall, 
    headers: { 
     'X-DocuSign-Authentication': jsonLoginCall 
    }, 
    xhrFields: { 
     withCredentials: true 
    }, 
    success: function() { 
     alert("success"); 
    }, 
    error: function() { 
     alert("failure"); 
    } 
}); 

通過菲德勒,我總是得到這樣的請求頭:

OPTIONS /restapi/v2/login_information HTTP/1.1 
Host: demo.docusign.net 
Connection: keep-alive 
Access-Control-Request-Method: POST 
Origin: https://twmts.quickbase.com 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36 
Access-Control-Request-Headers: x-docusign-authentication 
Accept: */* 
Referer: https://twmts.quickbase.com/db/bhkhmzax6?a=dbpage&pageID=145 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 

一個以此爲響應頭:

HTTP/1.1 200 OK 
Cache-Control: no-cache 
Date: Mon, 10 Mar 2014 12:15:48 GMT 
Content-Length: 0 
Strict-Transport-Security: max-age=7776000; includeSubDomains 

沒有XML或返回JSON。 我知道它必須是簡單的我只是沒有得到,但我花了很多時間tryig來確定我缺少什麼,而不是使用node.js,我無法弄清楚。

回答

1

你引用的的DocuSign示例代碼使用Node.js模塊,更具體地說,它採用requestasyncfs模塊。它看起來像你註釋掉它進口它所需要的Node.js庫三條線:

//var  async = require("async"),  // async module 
//  request = require("request"),  // request module 
//  fs = require("fs");   // fs module 

require聲明加載不同的模塊,當你註釋掉的代碼上面有不知道的async.waterfall函數是因爲它沒有在任何地方聲明。要解決上述代碼的解決方法,請使用以下代碼:

var  async = require("async"),  // async module 
     request = require("request"),  // request module 
     fs = require("fs");   // fs module 
+0

謝謝!我嘗試過也沒有運氣。我的目標是從QuickBase代碼頁訪問API,所以我無法安裝node.js.看到後續嘗試。謝謝! – user3389373