我有一个 Azure FHIR 服务器,我已经使用 POSTMAN 对其进行了测试,我能够使用 POSTMAN 和 Oauth token 读取患者以及 POST 患者。
我现在创建了一个 JavaScript Post 方法,以便我可以通过自己的方法而不是使用 POSTMAN 发布数据。 我遵循了这里的基本指导Creating a resource 我可以使用 POSTMAN 获取访问 token ,并在 POST 方法中使用该访问 token 。
我的方法和发布数据如下。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
(async () => {
const rawResponse = await fetch('https://smarthealth.azurehealthcareapis.com/Patient', {
method: 'POST',
Authorization: 'Bearer DGRijc8LrnjCrhRFRSn2Jnf89q2wGT_gxNHtJt',
headers: {
'Access-Control-Allow-Origin': '*',
'Accept': 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': 'http://localhost:3000',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS'
},
body: JSON.stringify({
"resourceType": "Patient",
"id": "9184e31a-a2b3-4b8d-9032-d5377cabaa08",
"extension": [
{
"url": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-race",
"extension": [
{
"url": "ombCategory",
"valueCoding": {
"system": "urn:oid:2.16.840.1.113883.6.238",
"code": "2106-3",
"display": "White"
}
},
{
"url": "text",
"valueString": "White"
}
]
},
{
"url": "http://hl7.org/fhir/StructureDefinition/patient-birthPlace",
"valueAddress": {
"city": "Mashpee",
"state": "Massachusetts",
"country": "US"
}
},
],
"name": [
{
"use": "official",
"family": "Kutch",
"given": [
"Sherron"
]
}
],
"gender": "female",
"birthDate": "2009-04-29",
})
});
const content = await rawResponse.json();
console.log(content);
})();
</script>
</head>
<body>
Javascript Azure FHIR Post Request Test
</body>
</html>
最佳答案
您可以尝试按照以下格式发送您的请求。
$("#btnPostToAzureFHIR").click(function () {
var yourPostDataFormat = {
resourceType: "Patient",
id: "9184e31a-a2b3-4b8d-9032-d5377cabaa08"
}
$.ajax({
type: "POST",
url: "https://smarthealth.azurehealthcareapis.com/Patient",
data: JSON.stringify(yourPostDataFormat),
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization','Bearer DGRijc8LrnjCrhRFRSn2Jnf89q2wGT_gxNHtJt');
},
dataType: "json",
contentType: "application/json",
success: function (data) {
console.log(data);
}
});
Note: Bind your parameter in key value pair on
yourPostDataFormat
then pass onJSON.stringify(yourPostDataFormat)
section. Hope it would post your request with token
关于javascript - 发布到 Azure FHIR 服务器方法失败身份验证失败 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60192848/