Reputation: 2192
I'm trying to just do a simple get_survey_list call - but it always returns an error: "No JSON object could be decoded: line 1 column 0 (char 0)"
Here's the code: (with my api key and oauth codes censored)
$(function(){
$.ajax({
url:'https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=XXXXXXXX',
type:'post',
contentType:'application/json',
dataType:'json',
headers:{
Authorization:'bearer XXXXXXXXXXXXXXXX',
'Content-Type':'application/json',
},
complete:function(jqXHR, textStatus){
console.log(jqXHR.responseJSON);
}
});
});
Am I missing something? When I try doing CURLing it, it works:
curl -H 'Authorization:bearer XXXXXXXXXXX' -H 'Content-Type: application/json' https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=XXXXXXXXX --data-binary '{}'
... which is annoying.
Upvotes: 1
Views: 711
Reputation: 1
If you get an error was: "Expected object or value" message in "SurveyMonkey's API " this code is working for you.
$.ajax({
url: "https://api.surveymonkey.net/v2/surveys/get_survey_details?api_key=xxxxxxxxxxxxx",
type: "post",
contentType: "application/json;",
dataType: "json",
data: JSON.stringify({ "survey_id": "xxxxxxxx" }),
headers: {
Authorization: "bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.J=",
"Content-Type": "application/json"
},
complete: function (jqXHR, textStatus, body) {
console.log(jqXHR.responseJSON);
}
});
if you have any quires please ask to SurveyMonkey's API team, they will contact to you "[email protected]" https://developer.surveymonkey.com/
Upvotes: 0
Reputation: 565
SurveyMonkey's API endpoints all require a JSON-encoded POST body. For get_survey_list, it's valid for no data to be contained in the POST body. When doing so, the API will return an array listing all of the survey IDs for the account you're accessing. You'll still need to send a JSON-encoded POST body, however; it will just be empty ("{}").
In your example, this minimal change should provide the results you're looking for:
$(function(){
$.ajax({
url:'https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=XXXXXXXX',
type:'post',
contentType:'application/json',
dataType:'json',
data: '{}'.
headers:{
Authorization:'bearer XXXXXXXXXXXXXXXX',
'Content-Type':'application/json',
},
complete:function(jqXHR, textStatus){
console.log(jqXHR.responseJSON);
}
});
});
In general, you'll build your POST bodies as javascript objects and use JSON.stringify() to encode them. This post might be helpful for understanding that process: Jquery Ajax Posting json to webservice
If you want to see the title of your surveys returned along with their IDs, you could do something like this:
$(function(){
$.ajax({
url:'https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=XXXXXXXX',
type:'post',
contentType:'application/json',
dataType:'json',
data: JSON.stringify({fields:"title"}),
headers:{
Authorization:'bearer XXXXXXXXXXXXXXXX',
'Content-Type':'application/json',
},
complete:function(jqXHR, textStatus){
console.log(jqXHR.responseJSON);
}
});
});
Upvotes: 4