【发布时间】:2016-09-16 18:58:12
【问题描述】:
JQuery for Angular2 中是否有类似 $.param() 的函数?
我知道 Angular 1 专门有一个类似的服务,Angular1 Equivalent
我检查了 Angular 2 站点 here,他们有一个 POST 演示,但他们在请求正文中发送了一个 JSON。
我当前的,不工作的,尝试
saveEdits() {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
this._http.post("ajax/update-thingy", JSON.stringify(this.dataJson), options)
.map(this.extractData)
.toPromise()
.catch(this.handleError);
// update in Angular 1 using JQuery function
// $http({
// method : 'POST',
// url : 'ajax/update-thingy',
// data : $.param($scope.formData),
// headers: {'Content-Type': 'application/x-www-form-urlencoded'}
// });
}
extractData(res: Response) {
let body = res.json();
if (body === 'failed') {
body = [];
}
return body || [];
}
private handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Promise.reject(errMsg);
}
或者,如果在我的 Angular2 中更容易获得 JQuery,也许这是一种方法。
更新
我确实解决了我自己的问题。请看我的回答below!
【问题讨论】: