Reputation: 1512
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.
Upvotes: 1
Views: 2003
Reputation: 1512
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....
Upvotes: 1