SurveyMonkey 入门 api

Getting started with SurveyMonkey api

我正在尝试开始使用 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)

我收到一条错误消息:

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.

我正在查看有关 CORS 的几个不同页面,但无法弄清楚下一步是什么。有什么建议吗?

继续挥舞 我一直在尝试找出 jsonp——但似乎是 can't handle the authorization headers

我还尝试使用 FormData 来包含额外的授权和选项 following this thread。它会连接到 api,但会说找不到授权令牌。

我越来越接近以下代码:

    $.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)

这实际上是连接和验证,但随后它 returns 状态为 3 "Expected object or value"。 Status codes documented here.

所以在敲了一整天之后,我终于让它工作了。我使用带有 beforeSend 的 jqueries post 来处理身份验证,并使用 JSON.stringify() 来处理请求的主体。最终代码如下:

    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)

现在我需要弄清楚身份验证持续多长时间,以及当我尝试深入了解特定信息时会发生什么....