To get in touch with plugin development, I am working on a small project to display visitor statistics in the panel. The data is coming from an external service. To start with, I largely follow the guides My first Panel view and Plugin setup for Panel plugins.
One deviation is that the data doesn’t get fetched by Remote::get()
. Instead I use a direct curl-request within my route:
$ch = curl_init($requestUrl);
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Api-Key: ' . $this->apiKey
)
]);
return curl_exec($ch);
This works fine. When I open the route in the browser via URL, the requested data gets returned:
{
"ok": true,
"start": "2021-01-26T23:00:00.000Z",
"end": "2021-02-26T23:59:59.999Z",
"timezone": "Europe/Berlin",
"pageviews": 168,
"generated_in_ms": 22
}{
"status": "ok",
"message": "ok",
"code": 200
}
But when I call the route in the vue component, the following error occurs:
The JSON response of the API could not be parsed:
{ "ok": true, "start": "2021-01-26T23:00:00.000Z", "end": "2021-02-26T23:59:59.999Z", "timezone": "Europe/Berlin", "pageviews": 168, "generated_in_ms": 48 }{"status":"ok","message":"ok","code":200}
So the expected data does get returned but can’t be parsed at the same time.
My vue component uses get()
like suggested in the guide:
methods: {
load() {
this.$api
.get("test")
.then(pageviews => {
this.pageviews = pageviews;
});
}
}
Any leads as to what is causing the error?