- Posts: 3
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Calling API with jQuery/Javascript Version 2.0
- FrAgFo0d
-
Topic Author
- Offline
- New Member
-
Less
More
10 years 6 months ago #93984
by FrAgFo0d
Calling API with jQuery/Javascript Version 2.0 was created by FrAgFo0d
Has anyone here have any knowledge about using jQuery to connect to the API?
I am using Limesurvey version 2.00 and setup the JSON-RPC.
I did a test on the API with http://10.0.0.2/index.php/admin/remotecontrol/test
The response I get is: "Invalid user name or password"
That seems O.K. to me.
But I tried several things with jQuery using $.ajax() and $.post()
trying to use: get_session_key however the API is non responsive.
So can anyone help me on the way with a small example of how this should work?
I am using Limesurvey version 2.00 and setup the JSON-RPC.
I did a test on the API with http://10.0.0.2/index.php/admin/remotecontrol/test
The response I get is: "Invalid user name or password"
That seems O.K. to me.
But I tried several things with jQuery using $.ajax() and $.post()
trying to use: get_session_key however the API is non responsive.
So can anyone help me on the way with a small example of how this should work?
The topic has been locked.
- FrAgFo0d
-
Topic Author
- Offline
- New Member
-
Less
More
- Posts: 3
- Thank you received: 0
10 years 6 months ago #94002
by FrAgFo0d
Replied by FrAgFo0d on topic Calling API with jQuery/Javascript Version 2.0
So here is some more info.
This doesn't work (yet).
I do get a success from jQuery, however there is no response what so ever from the API (in Firebug).
Which basically means that jQuery did load the page, but the page was blank.
This doesn't work (yet).
I do get a success from jQuery, however there is no response what so ever from the API (in Firebug).
Which basically means that jQuery did load the page, but the page was blank.
Code:
var params = { "method": "get_session_key", "params": { "username" : "admin", "password" : "test" }, id: 1, jsonrpc: "2.0" }; $.ajax({ url: "http://LimeSurvey/index.php/admin/remotecontrol", type: "POST", contentType: "application/json", data: JSON.stringify(params), error: function(data) { console.log('error'); console.log(data); }, success: function(data) { console.log('success'); console.log(data); } });
The topic has been locked.
- FrAgFo0d
-
Topic Author
- Offline
- New Member
-
Less
More
- Posts: 3
- Thank you received: 0
10 years 6 months ago #94018
by FrAgFo0d
Replied by FrAgFo0d on topic Calling API with jQuery/Javascript Version 2.0
OK So I am also checking it out in the Dutch forum.
Somehow I can send the data to the API, but there is just no response from the API.
I checked my HTML Request headers and my POST body in Firebug and get this:
This is my Response Header:
I do not get any response body.
Could anybody help me out with this?
Somehow I can send the data to the API, but there is just no response from the API.
I checked my HTML Request headers and my POST body in Firebug and get this:
Code:
Accept */* Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Content-Length 83 Content-Type application/json; charset=UTF-8 Host 10.0.0.2 Referer http://10.0.0.2/www/ User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:19.0) Gecko/20100101 Firefox/19.0 X-Requested-With XMLHttpRequest POST body: {"method":"get_session_key","params":{"username":"admin","password":"test"},"id":1}
This is my Response Header:
Code:
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Connection Keep-Alive Content-Encoding gzip Content-Length 20 Content-Type text/html Date Tue, 19 Mar 2013 15:23:41 GMT Expires Thu, 19 Nov 1981 08:52:00 GMT Keep-Alive timeout=15, max=99 Pragma no-cache Server Apache/2.2.16 (Debian) Vary Accept-Encoding X-Powered-By PHP/5.3.3-7+squeeze15
I do not get any response body.
Could anybody help me out with this?
The topic has been locked.
- Mazi
-
- Offline
- Official LimeSurvey Partner
-
5 years 4 months ago #169268
by Mazi
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
Replied by Mazi on topic Calling API with jQuery/Javascript Version 2.0
I know this is stone old but my findings may help others: I tested your code and faced the same problem. I then delete "id: 1," above " jsonrpc: '2.0' " and that worked for me, the message returned then is "success".
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
- Mazi
-
- Offline
- Official LimeSurvey Partner
-
5 years 4 months ago #169269
by Mazi
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
Replied by Mazi on topic Calling API with jQuery/Javascript Version 2.0
It looks like there is a bug at the old remote control API of Limesurvey 2.05/2.06.
Using the same code with Limesurvey 3.7 returns the proper session key but only if you add the "id: 1" details to the above code.
Really weird.
Using the same code with Limesurvey 3.7 returns the proper session key but only if you add the "id: 1" details to the above code.
Really weird.
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
- tpartner
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 10356
- Thank you received: 3585
5 years 4 months ago #169279
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Calling API with jQuery/Javascript Version 2.0
It's probably considerably more flexible to AJAX to a PHP file that access the API. This will allow you to use multiple methods and sanitize vars and data.
The downside is that you may not be able to upload PHP files through the theme editor.
The downside is that you may not be able to upload PHP files through the theme editor.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- Mazi
-
- Offline
- Official LimeSurvey Partner
-
5 years 4 months ago - 5 years 4 months ago #169306
by Mazi
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
Replied by Mazi on topic Calling API with jQuery/Javascript Version 2.0
Yes, Tony, that would surely be more easy, but at my current use case, PHP is no option.
Here comes some sample code I simply added at the first question of my test survey. Note that I am no JS expert so any hints on how to improve it are welcome!
Current challenge I am facing is turning the base64 encoded API response of the "export_responses" API function into a proper JSON format I can further process. Any hints?
Here comes some sample code I simply added at the first question of my test survey. Note that I am no JS expert so any hints on how to improve it are welcome!
Code:
<script> var params = { "method": "get_session_key", "params": { "username" : "ADD_USER_NAME_HERE", "password" : "ADD_PW_HERE" }, jsonrpc: "2.0", id: 1 }; var sessionid; $.ajax({ url: "https://ADD_YOUR_DOMAIN_HERE.com/index.php/admin/remotecontrol", type: "POST", contentType: "application/json", data: JSON.stringify(params), error: function(data) { console.log('error'); console.log(data); }, success: function(data) { console.log('success'); console.log(data); sessionidarray = JSON.parse(data); sessionid = sessionidarray['result']; console.log("Session key: "+sessionid); var fields = ['id', 'submitdate']; var params = { "method": "export_responses", "params": { "sessionkey" : sessionid , "surveyid" : "{SID}", "documenttype" : "json", "language": null, "status": "complete", "sHeadingType": 'code', "sResponseType": 'short' }, jsonrpc: "2.0", id: 1 }; $.ajax({ url: "https://ADD_YOUR_DOMAIN_HERE.com/index.php/admin/remotecontrol", type: "POST", contentType: "application/json", data: JSON.stringify(params), error: function(data) { console.log('error'); console.log(data); }, success: function(data) { console.log('success'); var jsonoutput =jQuery.getJSON(data); console.log(jsonoutput); } }); } }); </script>
Current challenge I am facing is turning the base64 encoded API response of the "export_responses" API function into a proper JSON format I can further process. Any hints?
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
Last edit: 5 years 4 months ago by Mazi.
The topic has been locked.
- tpartner
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 10356
- Thank you received: 3585
5 years 4 months ago - 5 years 4 months ago #169340
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Calling API with jQuery/Javascript Version 2.0
You could use atob() -
www.w3schools.com/jsref/met_win_atob.asp
- but it only works in IE10+.
Having said that, if you place this in a survey question, I think you may run into problems with the CSRF token, even if you release the session key as shown below.
Having said that, if you place this in a survey question, I think you may run into problems with the CSRF token, even if you release the session key as shown below.
Code:
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ var rpcURL = 'https://pathTo/limesurvey/index.php/admin/remotecontrol'; var params = { "method": "get_session_key", "params": { "username" : "admin", "password" : "password" }, id: 1 }; var sessionid; $.ajax({ url: rpcURL, method: "POST", contentType: "application/json", data: JSON.stringify(params), dataType : 'json', error: function(data) { console.log('error'); }, success: function(data) { console.log('Success'); console.log(data); sessionid = data.result; console.log("Session key: "+sessionid); var params = { "method": "export_responses", "params": { "sessionkey" : sessionid , "surveyid" : "{SID}", "documenttype" : "json", "language": null, "status": "complete", "sHeadingType": 'code', "sResponseType": 'short' }, id: 1 }; $.ajax({ url: rpcURL, method: "POST", contentType: "application/json", data: JSON.stringify(params), dataType : 'json', error: function(data) { console.log('error'); }, success: function(data) { console.log('Survey Responses:'); console.log(atob(data.result)); // Release the session key var params = { "method": "release_session_key", "params": { "sessionkey" : sessionid }, id: 1 }; $.ajax({ url: rpcURL, method: "POST", contentType: "application/json", data: JSON.stringify(params), dataType : 'json', error: function(data) { console.log('error'); }, success: function(data) { console.log('Key released:'); console.log(data); } }); } }); } }); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 4 months ago by tpartner.
The topic has been locked.
- Mazi
-
- Offline
- Official LimeSurvey Partner
-
5 years 4 months ago #169384
by Mazi
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
Replied by Mazi on topic Calling API with jQuery/Javascript Version 2.0
Thanks for the hints, Tony!
What are the options for non IE-10 users?
What are the options for non IE-10 users?
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
- tpartner
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 10356
- Thank you received: 3585
5 years 4 months ago #169385
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Calling API with jQuery/Javascript Version 2.0
There are several solutions mentioned here -
stackoverflow.com/questions/2820249/base...ient-side-javascript
(I have not had time to test any)
(I have not had time to test any)
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- Mazi
-
- Offline
- Official LimeSurvey Partner
-
5 years 4 months ago #169430
by Mazi
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
Replied by Mazi on topic Calling API with jQuery/Javascript Version 2.0
Thanks a lot, really helpful (as always).
Did you test if Chrome now support "atob()"? To me it looks like that works fine as well, see www.w3schools.com/jsref/met_win_atob.asp
Did you test if Chrome now support "atob()"? To me it looks like that works fine as well, see www.w3schools.com/jsref/met_win_atob.asp
Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
- tpartner
-
- Offline
- LimeSurvey Community Team
-
Less
More
- Posts: 10356
- Thank you received: 3585
5 years 4 months ago #169431
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Calling API with jQuery/Javascript Version 2.0
I tested atob() in FF and Chrome - worked as expected in both.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.