2017-06-21 24 views
1

的內容類型的HTTP標頭的HTTP POST字符串值是application/x-www-form-urlencoded在身體

我必須發佈一個字符串值。 environmentId: "predevnet"

我在去年的項目,我用jQuery來進行Ajax調用:

$.ajax({ 
     headers: this.headers, 
     type: this.type, 
     url: this.url, 
     data: {environmentId: "predevnet"}, 
     dataType: this.dataType, 
     contentType: this.contentType, 
     async: isAsync, 
     success: success, 
     cache: this.cache, 
     error: error 
    }); 

現在我試圖做同樣的呼叫角

return this.http 
    .post(this.baseUrl + action, JSON.stringify({environmentId: "predevnet"}), options) 
    .map(response => response.json() as DcResponse<T>);` 

結果預計:表單數據應該是這樣的:Result Expected

和我得到的結果和沒有JSON.stringify是這樣的:Current results

+0

請問,如果你設置它的工作內容類型頭應用程序/ JSON? –

+0

不,我試過了,但它發送了一個json對象..它只能發送名稱爲 –

+0

的變量你試過使用'toString()'而不是'JSON.stringify()'嗎? – Alex

回答

0

當您只想發送一個鍵值對作爲POST請求正文中的參數時,必須省略鍵部分。

所以,如果你想要的內容類型爲application/x-www-form-urlencoded那麼你的例子已經是這樣的:

return this.http 
    .post(this.baseUrl + action, '=predevnet', options) 
    .map(response => response.json() as DcResponse<T>);` 

否則,如果你喜歡application/json那麼你就寫:

return this.http 
    .post(this.baseUrl + action, '"predevnet"', options) 
    .map(response => response.json() as DcResponse<T>);`