Hi,
I´ve been struggeling for a week but I cant get this to work. My sites are handling multiple API calls from multiple sources but there´s this one which I cant get to work (I´m defeated). Essentially I´m trying to build an authorisation flow where the user can use Swedish Bank ID to login to the site.
First of all the error message:
“result”:{“message”:“invalid json response body at https://testbed-eid.scrive.com/api/v1/transaction/new reason: Unexpected token < in JSON at position 0”,“name”:“FetchError”,“stack”:“”,“_elementoryError”:true},“exception”:true}
Then the code:
// BACK_END
import {fetch} from ‘wix-fetch’;
const base = “https://testbed-eid.scrive.com/api/v1”;
let access_token = “1234567-123.132-123123-123213”
let redirecturl = “https://www.mylandingpage.se/”;
let provider = “seBankID”;
let method = “auth”
export async function BackendAPICall() {
const body = {
redirectUrl : redirecturl,
provider : provider,
method : method
};
const response = await fetch(base + “/transaction/new”, {
method: “POST”,
body: Object.keys(body)
.map(key => encodeURIComponent(key) + “=” + encodeURIComponent(body[key]))
.join( “&”),
headers: {
“Content-Type”: “application/x-www-form-urlencoded;charset=UTF-8”,
“Authorization”: "Bearer " + access_token
},
mode : “cors”
})
. catch ((err) => {
let errorMsg = err;
console.log(errorMsg);
});
return handleResponse(response);
}
async function handleResponse(response) {
const json = await response.json();
if (response.status !== 200) {
throw new Error(json.errorMessage);
}
return json;
}
Then in the front-end I´m just having a button like this:
export function button15_click(event) {
BackendAPICall()
}