我正在尝试使用如下角度的http post方法发布数据。但是,每当我执行此操作时,它都会执行get方法。任何建议或意见将不胜感激。
login() {
let headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let data = {
"id" : this.phoneNumber,
"uuid": '1234123412341234'
}
const body = new URLSearchParams();
Object.keys(data).forEach(key => {
body.set(key, data[key]);
})
let token = 'a%2FxMRIEsjokCR6vBkmpjh2UBeRKlVxLEiXundnAoqz0%3D';
var url = '/api/auth/login?origin=driver&access-token='+token;
this.http.post(url, body.toString(), options).subscribe( data =>{
console.log(data);
})
}
我用角度1编码了类似的东西。
$scope.login = function(){
var link = proxy_link+'api/auth/login';
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$http({
method: 'POST',
url: link,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
params: {
'device': 'android',
},
data: {
id:$scope.data.id,
pw:$scope.data.pw,
device_token: localStorage.myPush,
uuid : localStorage.uuid,
imei: localStorage.imei
}
}).success(function(data){
// do something
}).error(function(data){
// handle error
});
};
答案 0 :(得分:1)
查看您的angular 1代码,您需要在POST表单数据中发送所有参数,而不是在url中。
尝试:
login() {
let headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let token = 'a%2FxMRIEsjokCR6vBkmpjh2UBeRKlVxLEiXundnAoqz0%3D';
//set it here
let data = {
"id" : this.phoneNumber,
"uuid": '1234123412341234',
"origin":driver,
"access_token":token
}
const body = new URLSearchParams();
Object.keys(data).forEach(key => {
body.set(key, data[key]);
})
var url = '/api/auth/login';
this.http.post(url, body, options).subscribe( data =>{
console.log(data);
})
}