1

I'm trying to get started with using the SurveyMonkey api.

    $.ajax({
        method:"POST",
        url:"https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key="+apiKey,
        headers:{
          "Authorization": "bearer "+token,
          "Content-Type": "application/json"
        },
        body:{
          "fields": [
            "title",
            "analysis_url",
            "preview_url",
            "date_created",
            "date_modified",
            "question_count",
            "num_responses"
          ]
        }   
    })
    .success(createListPicker)
    .error(handleError)

I get an error message:

XMLHttpRequest cannot load https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=bs579cpsb4mnvn4vh6uqvp2m. The request was redirected to 'https://api.surveymonkey.net/v2/surveys/get_survey_list/?api_key=bs579cpsb4mnvn4vh6uqvp2m', which is disallowed for cross-origin requests that require preflight.

I'm looking at several different pages about CORS, but can't figure out what the next step is. Any advice?

The flailing continues I've continued to try and figure out jsonp -- but that seems it can't handle the authorization headers

I've also tried to use FormData to included the extra authorizations and options following this thread. It would connect to the api, but then say it couldn't find the authorization token.

I am getting closer with the following code:

    $.ajax({
        type:"POST",
        url:"https://api.surveymonkey.net/v2/surveys/get_survey_list/?api_key="+apiKey,
        beforeSend: function(xhr) {
                        xhr.setRequestHeader('Authorization','bearer ' + token);
                        xhr.setRequestHeader('Content-Type','application/json');
                    } 
    })
     .success(createListPicker)
     .error(handleError)

That actually connects and authenticates, but then it returns a status of 3 "Expected object or value". Status codes documented here.

Community
  • 1
  • 1
Rothrock
  • 1,413
  • 2
  • 16
  • 39
  • Have you tried updating your request URL to the one you're being redirected to? (It has an extra `/` before the `?`) - could just be a small misread of the docs, resulting in a redirect that subsequently doesn't work? – James Thorpe Dec 03 '15 at 19:57
  • To perform a CORS request, you have to use jsonP. http://stackoverflow.com/questions/5943630/basic-example-of-using-ajax-with-jsonp – sudheer Dec 03 '15 at 20:02
  • James Thorpe, I just tried that and I get a 404 not found error. – Rothrock Dec 03 '15 at 20:14
  • sudheer -- I'm looking at that thread and don't understand it. Do you use the get_survey_list endpoint and api key and then have a callback function that requests it again? I guess I'm just not clear which parts go in the preflight and which parts then go to actually request the data I'm wanting... – Rothrock Dec 03 '15 at 20:18
  • I would use a different survey tool. Both Examinare developer.examinare.com and SurveyGizmo has a much easier API. – user2420647 Feb 21 '16 at 13:52
  • Unfortunately this is something beyond my control. We have years of survey data in SurveyMonkey and several different departments make use of it. – Rothrock Feb 21 '16 at 21:11

1 Answers1

1

So after banging on this for a full day I finally got it working. I used jqueries post with a beforeSend to handle the authentication and JSON.stringify() to deal with the body of the request. Final code looks like:

    var obj = {
          "fields": [
            "title",
            "date_created",
            "date_modified",
            "num_responses"
          ],
          "start_date":"2015-12-01 00:00:00"
        }

    $.ajax({
        type:"POST",
        dataType:"json",
        contentType:'application/json; charset=utf-8',
        url:"https://api.surveymonkey.net/v2/surveys/get_survey_list/?api_key="+apiKey,
        data:JSON.stringify(obj),
        beforeSend: function(xhr) {
                        xhr.setRequestHeader('Authorization','bearer ' + token);
                        xhr.setRequestHeader('Content-Type','application/json');
                    } 
    })
     .success(createListPicker)
     .error(handleError)

Now I need to work out how long the authentication lasts and what happens as I try to drill down to specific information....

Rothrock
  • 1,413
  • 2
  • 16
  • 39
  • Isn't exposing your Authorization token in client side JavaScript kind of risky? – Jake Wilson Jun 02 '17 at 16:54
  • Also, how did you get around CORS issues here? – Jake Wilson Jun 02 '17 at 16:55
  • The beforeSend seemed to be the answer. This was quite awhile ago, so I don't remember all the details. This was for an internal teamsite, so it is behind our company firewall. I wouldn't post that token online. – Rothrock Jun 02 '17 at 17:18