1

Call Wialon Remote API from angular http client

Тема: Call Wialon Remote API from angular http client

Hi, i want to call wialon remote API from angular http client, but i get the error "from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

this is my code

export class WialonAPIService {
rooturl = 'https://hst-api.wialon.com/wialon/ajax.html?svc=';
urlGetUnits = 'core/search_items&params=' +
'{"spec":{"itemsType":"avl_unit","propName":"sys_name","propValueMask":"*","sortType":"sys_name"},' +
'"force":1,"flags":1,"from":0,"to":0}&sid=0961c9cd63b4f328d32fe64760aae515';
  constructor(private http: HttpClient) { }

  getUnits() {
    console.log('get truck wialon');
    return this.http.get(this.rooturl + this.urlGetUnits)
    .pipe(map(res => res));
  }
}

It is necesary to call ir directly from the backend or i can call ir directly from my angular app?

2

Call Wialon Remote API from angular http client

Re: Call Wialon Remote API from angular http client

Currently Wialon does not sets Access-Control-Allow-Origin header and you can't call Request API directly from a web app. I think that we will add it somewhen, when will be sure, that it will not lead to security issues.

So, there is two ways to use Remote API:

  • Via wialon.js
  • Use post.html directly, which wialon.js uses underhood

For the second variant you need to:

1. Add https://hst-api.wialon.com/wialon/post.html to your page as invisible iframe.
2. Send post message to it with next json:

{"id": 0, "source": "https://hst-api.wialon.com/wialon/post.html", "chunkedPrefix": "someprefix:", "enableChunkedResult": true}

3. Wait for response:

{"id": 0, "source": "https://hst-api.wialon.com/wialon/post.html", "error": 0, "chunkedResult": true}

4. Send:

{"id": 1, "url": "/avl_evts", "params":"sid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "source":"https://hst-api.wialon.com/wialon/post.html"}

Where id must be unique, url can be /avl_evts or /wialon/ajax.html, params is request POST params.

5. Receive:

someprefix:1:0:1:{"id":1, "error": 0, "source":"https://hst-api.wialon.com/wialon/post.html", "text": RESPONSE_JSON}

Format is:

prefix : id : chunk index : chunks count : chunk data

Then you need to collect all chunks for id and then concatenate them in order of indexes.

-----------------------

Also, enableChunkedResult in step 2 can be set to false. Or chunkedResult in step 3 can be not true (not for hst-api, but for WL can), then responses in step 5 will contain whole response without prefixes/chunks.

But I will not recommend to use it, since browsers have limit for post messages and if you will receive huge response, it will crash.

3

Call Wialon Remote API from angular http client

Re: Call Wialon Remote API from angular http client

Hello in my experience you can create your own api based on wialon SDK PHP,Node JS, python, etc.
I recommend you use the remote api to make this task posibble, I know Angular has this problem in develop mode, I solved this issue by this way.

Regards.

4

Call Wialon Remote API from angular http client

Re: Call Wialon Remote API from angular http client

Hi, rual and cerpas, thanks for your answer.
Cerpas as an Angular developer do you recommend to call remote API on the backend(web api on dotnetcore 2.1)? because i just can't get working wialonjs-api on angular


Additional to this, i need to use the wialon authentication in the login of my site because i want the users to authenticate with his user and password of the gps wialon platform, do i need to open a window as in the wialon.js documentation (https://sdk.wialon.com/playground/demo/app_auth_token) or how can i achieve this?

Thank you a lot for your help! smile

5

Call Wialon Remote API from angular http client

Re: Call Wialon Remote API from angular http client

Hello.

I think you can use your prefered languaje to crete your backend, in my case I use languajes with sdk provided by wialon for exple PHP, NodeJS (usin wialon-js) but perfectly you can create your Back End in .net but in this you will create all request manual.

For your login question really I never need use this method beacuase normally I created apps to wialon or specifics scripts to create specifics task, but I can remember this method work with a POST request.

I hope this information help you.

andony.velazquez пишет:

Hi, rual and cerpas, thanks for your answer.
Cerpas as an Angular developer do you recommend to call remote API on the backend(web api on dotnetcore 2.1)? because i just can't get working wialonjs-api on angular


Additional to this, i need to use the wialon authentication in the login of my site because i want the users to authenticate with his user and password of the gps wialon platform, do i need to open a window as in the wialon.js documentation (https://sdk.wialon.com/playground/demo/app_auth_token) or how can i achieve this?

Thank you a lot for your help! smile

6

Call Wialon Remote API from angular http client

Re: Call Wialon Remote API from angular http client

Здравствуйте! Вопрос связан с ранее оставленным ответом о работе с Wialon remote API:  https://forum.gurtam.com/viewtopic.php? … 44#p158644

Осуществлять POST запрос нужно напрямую к дочернему iframe?
Я делаю это так:

[iframe элемент].contentWindow.postMessage({"id": 0, "source": "https://hst-api.wialon.com/wialon/post.html", "chunkedPrefix": "prefix:", "enableChunkedResult": true}, '*')

Приведённый в примере ответ под пунктом 3 не приходит. Сообщения я слушаю следующим образом:
       

window.addEventListener("message", evt => {
           if (evt.origin != "https://hst-api.wialon.com"){
               return;
           }
    ...
    }

С чем могут быть связаны данные проблемы?

7

Call Wialon Remote API from angular http client

Re: Call Wialon Remote API from angular http client

A2ro пишет:

С чем могут быть связаны данные проблемы?

Попробовал сам, набросал работающий пример — https://sdk.wialon.com/playground/yDOe6Uzx/1 (не до конца реализовывал)

Оказалось что post.html ожидает только строки, не объекты.

Не могу ничего обещать, но включение CORS не за горами и не нужно будет подобными штуками заниматься.

8

Call Wialon Remote API from angular http client

Re: Call Wialon Remote API from angular http client

Спасибо. Решили данный вопрос с помощью Вашей JS библиотеки.