firestore 正在拒绝来自 angularjs 的 post 请求
firestore is rejecting post request from angularjs
我正在尝试使用 angularjs 中的 $http.post
方法向集合 Offers 添加新文档。
我在控制台中收到此错误:
Possibly unhandled rejection: {
"data": {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unexpected token.\nfields%5BcompanyName\n^",
"status": "INVALID_ARGUMENT"
}
},
"status": 400,
"config": {
"method": "POST",
"transformRequest": [null],
"transformResponse": [null],
"jsonpCallbackParam": "callback",
"url": "https://firestore.googleapis.com/v1beta1/projects/syrian-sales-v2/databases/(default)/documents/Offers",
"data": "fields%5BcompanyName%5D=qwe&fields%5Bdescription%5D=qwe&fields%5BexpiredDate%5D=qwe&fields%5Btitle%5D=qwe&fields%5BuserId%5D=rsVWKar7UTMwUmcyYJbr6Rif4NN2",
"headers": {
"Content-Type": "application/json; charset=UTF-8;",
"Accept": "application/json, text/plain, /"
}
},
"statusText": "",
"xhrStatus": "complete"
}
我尝试了很多解决方案,但 none 奏效了。
控制器代码如下:
app.controller('insert_controller', function ($scope, $http, $httpParamSerializerJQLike) {
let userId = "rsVWKar7UTMwUmcyYJbr6Rif4NN2";
let url = "https://firestore.googleapis.com/v1beta1/projects/syrian-sales-v2/databases/(default)/documents/Offers";
$scope.insertOffer = function () {
let companyName = $scope.companyName;
let title = $scope.title;
let expiredDate = $scope.expiredDate;
let description = $scope.description;
let data = {
"fields": {
"companyName": companyName,
"title": title,
"expiredDate": expiredDate,
"description": description,
"userId": userId,
}
};
$http({
method: 'POST',
url: url,
data: $httpParamSerializerJQLike(data),
headers: {
'Content-Type': 'application/json; charset=UTF-8;'
}
}).then(function (res) {
if (res.data){
$scope.companyName = null;
$scope.title = null;
$scope.expiredDate = null;
$scope.description = null;
alert('Offer Added Successfully')
}
}).catch(function (err) {
console.log(err);
throw err
});
}
});
任何帮助都会很棒,提前致谢
EDIT:我尝试从 postman 发送一个 post 请求,它对相同的 url 和相同的 json 我正在使用的数据结构
问题是我使用了错误的架构。
这是旧架构:
let data = {
"fields": {
"companyName": companyName,
"title": title,
"expiredDate": expiredDate,
"description": description,
"userId": userId,
}
};
这是有效的正确模式:
let data = {
"fields": {
"companyName": {
"stringValue": companyName
},
"title": {
"stringValue": title
},
"expiredDate": {
"stringValue": expiredDate
},
"description": {
"stringValue": description
},
"userId": {
"stringValue": userId
},
}
};
我正在尝试使用 angularjs 中的 $http.post
方法向集合 Offers 添加新文档。
我在控制台中收到此错误:
Possibly unhandled rejection: {
"data": {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unexpected token.\nfields%5BcompanyName\n^",
"status": "INVALID_ARGUMENT"
}
},
"status": 400,
"config": {
"method": "POST",
"transformRequest": [null],
"transformResponse": [null],
"jsonpCallbackParam": "callback",
"url": "https://firestore.googleapis.com/v1beta1/projects/syrian-sales-v2/databases/(default)/documents/Offers",
"data": "fields%5BcompanyName%5D=qwe&fields%5Bdescription%5D=qwe&fields%5BexpiredDate%5D=qwe&fields%5Btitle%5D=qwe&fields%5BuserId%5D=rsVWKar7UTMwUmcyYJbr6Rif4NN2",
"headers": {
"Content-Type": "application/json; charset=UTF-8;",
"Accept": "application/json, text/plain, /"
}
},
"statusText": "",
"xhrStatus": "complete"
}
我尝试了很多解决方案,但 none 奏效了。
控制器代码如下:
app.controller('insert_controller', function ($scope, $http, $httpParamSerializerJQLike) {
let userId = "rsVWKar7UTMwUmcyYJbr6Rif4NN2";
let url = "https://firestore.googleapis.com/v1beta1/projects/syrian-sales-v2/databases/(default)/documents/Offers";
$scope.insertOffer = function () {
let companyName = $scope.companyName;
let title = $scope.title;
let expiredDate = $scope.expiredDate;
let description = $scope.description;
let data = {
"fields": {
"companyName": companyName,
"title": title,
"expiredDate": expiredDate,
"description": description,
"userId": userId,
}
};
$http({
method: 'POST',
url: url,
data: $httpParamSerializerJQLike(data),
headers: {
'Content-Type': 'application/json; charset=UTF-8;'
}
}).then(function (res) {
if (res.data){
$scope.companyName = null;
$scope.title = null;
$scope.expiredDate = null;
$scope.description = null;
alert('Offer Added Successfully')
}
}).catch(function (err) {
console.log(err);
throw err
});
}
});
任何帮助都会很棒,提前致谢
EDIT:我尝试从 postman 发送一个 post 请求,它对相同的 url 和相同的 json 我正在使用的数据结构
问题是我使用了错误的架构。
这是旧架构:
let data = {
"fields": {
"companyName": companyName,
"title": title,
"expiredDate": expiredDate,
"description": description,
"userId": userId,
}
};
这是有效的正确模式:
let data = {
"fields": {
"companyName": {
"stringValue": companyName
},
"title": {
"stringValue": title
},
"expiredDate": {
"stringValue": expiredDate
},
"description": {
"stringValue": description
},
"userId": {
"stringValue": userId
},
}
};