调查 JS 以 Json 而不是 XML 格式获得调查结果响应

问题描述

我正在尝试以 JSON 格式获取调查结果,但 API 以 XML 格式提供响应。 API 列出了 json 响应也可用,但是在发出请求时我必须使用哪个属性无处可寻。

https://surveyjs.io/Help/Api/GET-api-Surveys-getSurveyPublicResults-id_from_till?apiType=private&renderType=desktop

https://api.surveyjs.io/private/Surveys/getSurveyPublicResults/{id}?from={from}&till={till}

解决方法

我相信您需要确保您的请求有一个值为“application/json”的“Accept”标头。

例如,如果您使用 jQuery,您可以执行以下操作:

var settings = {
  "url": "https://api.surveyjs.io/private/Surveys/getSurveyPublicResults/{id}?from={from}&till={till}","method": "GET","timeout": 0,"headers": {
    "Accept": "application/json; charset=utf-8"
  },};

$.ajax(settings).done(function (response) {
  console.log(response);
});