2017-06-21 66 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>);`