如何使角2发送的所有请求的应用程序/ x-WWW的形式urlen codeD [英] How to make Angular 2 send all requests as application/x-www-form-urlencoded
问题描述
我曾与1角,所以我知道如何实现,只有我错过最后一步了类似的问题。
和上次一样,一个人谁对我们的应用程序做后台接受带有类型的请求应用程序/ x-WWW的形式urlen codeD
键,就像角1也是如此角2型向他们发送应用程序/ JSON
。
我所做的与NG1是,在配置我修改了HTTP提供超过每个请求的身体运行urlen codeD。
我在NG2看到有的https://角.IO /文档/ TS /最新/ API / HTTP / BaseRequestOptions-class.html
一个 BaseRequestOptions
,我认为正是这种唯一文档是由类是有点不存在
所以我不知道如何正确地实现这个(我也是新打字稿)。
我如何提供这个让我的每个帖子
和其他请求将发送如 urlen codeD
末(我也想函数中对身体运行,因此,它实际上变成urlen codeD)。
另外:为什么没有这个更简单的选择,因为现在我可以看到,无论ASP.Net和瓶(所以我想一些其他的方面)不支持应用程序\\ JSON
默认?
编辑:我做了,我的每一个对象在我身上POST发送上使用自定义函数,但我希望有一个更简单,更通用的解决方案。
从'angular2 / HTTP'进口{URLSearchParams};导出功能urlEn code(OBJ:对象):字符串{
让urlSearchParams =新URLSearchParams();
对(让OBJ键){
urlSearchParams.append(键,OBJ [关键]);
}
返回urlSearchParams.toString();
}
然后我使用它像
this.http.post(的http://本地主机:5000 /用户/认证/注册,urlEn code(数据))
尝试是这样的:
DefaultRequestOptions.ts
@Injectable()
出口类DefaultRequestOptions扩展BaseRequestOptions {
标题:标题=新的报头({
内容类型:应用程序/ x-WWW的形式urlen codeD
});
}
boot.ts
引导(AppComponent,[
HTTP_PROVIDERS,
提供(RequestOptions,{useClass:DefaultRequestOptions})
提供(HTTP,{useFactory:
功能(后端,defaultOptions){
返回新的HTTP(后端,defaultOptions); },
DEPS:[XHRBackend,RequestOptions]})
]);
重要提示:请务必不要在组件再次声明HTTP_PROVIDERS在这里您可以使用HTTP对象或它会覆盖您在boot.ts被注入一个
有关其他的功能你可以将其添加到任何@Injectable对象,添加对象插入到组件的构造函数,你可以把它从任何组件的方式,或者你可以尝试扩展HTTP角对象,并覆盖为了post方法做了幕后每一个岗位的要求。
I have had a similar problem with Angular 1 so I understand how to implement, only I'm missing the last step.
Just like last time, a guy who did backend for our app accepts requests with type application/x-www-form-urlencoded
and just like Angular 1 so does Angular 2 send them with type application/json
.
What I did with ng1 was that in config I modified the http provider to run urlencoded over body of each request.
I see in ng2 that there is https://angular.io/docs/ts/latest/api/http/BaseRequestOptions-class.html
a BaseRequestOptions
class which I supposed is made for exactly this only documentation is a bit not there
so I'm not sure how to implement this properly (I'm also new to TypeScript).
How do I provide this so that each of my post
and other requests get send as urlencoded
at the end (I also want the function to be run on the body so that it actually becomes urlencoded).
Also: Why isn't there a simpler option for this since by now I can see that both ASP.Net and Flask (so I suppose many others as well) do not support application\json
by default?
EDIT: I have made a custom function which I use on every object I send in POST body but I hope that there is a simpler, more general solution.
import { URLSearchParams } from 'angular2/http';
export function urlEncode(obj: Object): string {
let urlSearchParams = new URLSearchParams();
for (let key in obj) {
urlSearchParams.append(key, obj[key]);
}
return urlSearchParams.toString();
}
and then I use it like
this.http.post('http://localhost:5000/user/auth/login', urlEncode(data))
Try something like this:
DefaultRequestOptions.ts
@Injectable()
export class DefaultRequestOptions extends BaseRequestOptions{
headers:Headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
}
boot.ts
bootstrap(AppComponent,[
HTTP_PROVIDERS,
provide( RequestOptions, { useClass: DefaultRequestOptions } ),
provide(Http, { useFactory:
function(backend, defaultOptions) {
return new Http(backend, defaultOptions); },
deps: [XHRBackend, RequestOptions]})
]);
IMPORTANT: Make sure you don't declare HTTP_PROVIDERS again in the component where you'll use the Http object or it'll override the one you are injecting in boot.ts.
For the other function you can just add it into any @Injectable object, add that object into the constructor of your components and you can call it that way from any component, or you can try to extend the http angular object and override the post method in order to do it for every post request behind the scenes.
这篇关于如何使角2发送的所有请求的应用程序/ x-WWW的形式urlen codeD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!