Question T7227
Visible to All Users

Change URL (with parameters) on ChoicesRestfull

created 4 years ago

Hello everyone,
I am currently developing javascript webcomponents with surveyjs for multi-platform implementation.
Since I'm working with different hosts and urls I'd need to dynamically change the url based on the server I'm on.
For example:
I have the generic API url of a dropdown question: /generic/api/url?param1={question1}&param2=constant
I need to change that url based on the backend server so for example:
If I'm on x.y.z.w/server1/getSurvey?id=1, my ChoicesRestfull questions will have the /server1 prefix on their urls: /server1/generic/api/url?param1={question1}&param2=constant
And this will go for other servers as well:
/server2/generic/api/url?param1={question1}&param2=constant
/server3/generic/api/url?param1={question1}&param2=constant
[…]
Now, I know that you can intercept the XMLHttpRequest with:

JavaScript
Survey.ChoicesRestfull.onBeforeSendRequest = function(sender, options) { // do stuff here };

But there's no way to access the processedUrl variable that contains the already parsed URL with the query parameters already set, I can only access the url variable from the sender object. This way I cannot change the url, because what I get still contains the unparsed parameters.

I just wanted to know if I can do this with the already provided Choices widget or I'll have to create a custom widget.
Thank you

Answers approved by surveyjs Support

created 4 years ago

Hello Luca,
Why don't you update the url directly in the property on loading survey model for example?
You can have a generic url path and then change it based on you logic after survey model is loaded.

Thank you,
Andrew
SurveyJS Team