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
  • FrAgFo0d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 week ago #93984 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?
The topic has been locked.
  • FrAgFo0d
  • FrAgFo0d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 week 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.
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
  • FrAgFo0d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 week 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:
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
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 9 months ago #169268 by Mazi
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
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 9 months ago #169269 by Mazi
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.

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
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169279 by tpartner
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.

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
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 9 months ago - 5 years 9 months ago #169306 by Mazi
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!
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 9 months ago by Mazi.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago - 5 years 9 months ago #169340 by tpartner
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.

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 9 months ago by tpartner.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 9 months ago #169384 by Mazi
Thanks for the hints, Tony!

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
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169385 by tpartner
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)

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
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 9 months ago #169430 by Mazi
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

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
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 9 months ago #169431 by tpartner
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.

Lime-years ahead

Online-surveys for every purse and purpose