2015-05-19 99 views
0

我正在使用Typescript開始一個新項目,從中我沒有以前的經驗。我想用d3.xhr方法做一些HTTP請求,但目前爲止沒有取得太大的成功。見下面的相關代碼段:無法使用d3類型觸發POST-JSON請求

private buildBody(query: string): any { 
    return { 
    statements: [{ 
     statement: query 
    }] 
    } 
} 

exec(query: string) { 
    d3.xhr(this.baseURL) 
    .header('Content-Type', 'application/json') 
    .post(buildBody(query), (error, data) => { 
     if (error){ 
     console.error(error); 
     } else { 
     console.log(data); 
     } 
    }); 
} 

這似乎是無效的。有一個看看D3類型的定義,有件事情我真的不明白:

post: { 
    /** 
    * Issue the request using the POST method 
    * 
    * @param callback Function to invoke on completion of request 
    */ 
    (callback?: (xhr: XMLHttpRequest) => void): Xhr; 
    /** 
    * Issue the request using the POST method 
    * 
    * @param data Data to post back in the request 
    * @param callback Function to invoke on completion of request 
    */ 
    (data: any, callback?: (xhr: XMLHttpRequest) => void): Xhr; 
}; 

POST請求回調預計(xhr: XMLHttpRequest) => void類型,而D3 API預計一些兩個參數的函數。 Typescript中通常定義的回調是怎樣的? post方法(回調類型)定義實際上是否正確?

在此先感謝。

回答

1
post: { 
    /** 
    * Issue the request using the POST method 
    * 
    * @param callback Function to invoke on completion of request 
    */ 
    (callback?: (xhr: XMLHttpRequest) => void): Xhr; 
    /** 
    * Issue the request using the POST method 
    * 
    * @param data Data to post back in the request 
    * @param callback Function to invoke on completion of request 
    */ 
    (data: any, callback?: (xhr: XMLHttpRequest) => void): Xhr; 
}; 

僅僅是一個告訴的JavaScript post是一個函數(一個功能),可以以兩種不同的方式調用的方式。

例如:

foo: function(a, b) { 

    if (b != null) 
     return a * 2 + b; 
    else 
     return a * 2; 
} 

foo功能以JavaScript兩種方式調用,e.g:

var a = foo(3); 
var b = foo(3, 4); 

上述調用FOO的都是有效的。告訴打字稿是這樣的話,我們可以創建一個定義,包含以下信息:

foo: { 
    (a: number): number; 
    (a: number, b: number): number 
} 
情況下

所以它只是說,後有一個方法,其可稱爲一種方式以不止一種方式。

這就是說,定義與api相比可能仍然存在問題。

編輯:

好了,所以我看着到D3 source,這是它是如何定義的:

// Convenience methods. 
["get", "post"].forEach(
    function(method) { 
     xhr[method] = function() { 
      return xhr.send.apply(xhr, [method].concat(d3_array(arguments))); 
     }; 
    }); 

// If callback is non-null, it will be used for error and load events. 
xhr.send = function(method, data, callback) { 
    if (arguments.length === 2 && typeof data === "function") callback = data, data = null; 
    request.open(method, url, true); 
    if (mimeType != null && !("accept" in headers)) headers["accept"] = mimeType + ",*/*"; 
    if (request.setRequestHeader) for (var name in headers) request.setRequestHeader(name, headers[name]); 
    if (mimeType != null && request.overrideMimeType) request.overrideMimeType(mimeType); 
    if (responseType != null) request.responseType = responseType; 
    if (callback != null) xhr.on("error", callback).on("load", function(request) { callback(null, request); }); 
    dispatch.beforesend.call(xhr, request); 
    request.send(data == null ? null : data); 
    return xhr; 
}; 

鑑於好像定義就是完全錯誤的。 callback以兩種方式被調用; callback(null, request);xhr.on("error", callback)。在請求傳遞給回調的情況下,請求的類型爲XMLHttpRequest。但是,定義是錯誤的。

+0

這是正確的,但不是我的問題。我的問題是回調類型('(xhr:XMLHttpRequest)=> void')是否正確,因爲有了這樣的定義,我無法觸發任何單個請求。我會更新我的問題,使之更清晰。 – jarandaf

+0

編輯我的答案來反映這一點。 – Nypan

+1

我創建了一個PR,看來類型定義確實是錯的。我會接受你的答案,謝謝你的時間。 – jarandaf