jsonp ajax in google script

六眼飞鱼酱① 提交于 2020-06-23 18:36:13

问题


I'm trying to learn how to query for data from a local government data site (hoping I can teach my math students to do some data analysis). I'm hoping to get the data and insert them into Google Sheets. The following is a sample provided by the official site on how to do a query:

var data = {
  resource_id: '1b702208-44bf-4829-b620-4615ee19b57c', // the resource id
  limit: 5, // get 5 results
  q: 'jones' // query for 'jones'
};
$.ajax({
  url: 'https://data.gov.sg/api/action/datastore_search',
  data: data,
  dataType: 'jsonp',
  success: function(data) {
    alert('Total results found: ' + data.result.total)
 }
});$

I tried the following code in Google Apps Script:

function testapi(){
  var data = {
    resource_id: '1b702208-44bf-4829-b620-4615ee19b57c', // the resource id
    limit: 5, // get 5 results
    q: 'Yishun' 
  };

  var url = "https://data.gov.sg/api/action/datastore_search";
  var response = UrlFetchApp.fetch(url,data).getContentText();
}

I receive a 404 error. I think the option "data" was not passed.

Would appreciate some help. I am a math teacher, not a coding expert.

Update: I changed the code to this but still 404 error.

function testapi(){
  var data = {
    resource_id: '1b702208-44bf-4829-b620-4615ee19b57c', // the resource id
    limit: 5, // get 5 results
    q: 'Yishun' // query for 'jones'
  };

  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify(data)
  };  

  var url = "https://data.gov.sg/api/action/datastore_search";
      var response = UrlFetchApp.fetch(url,options).getContentText();

  }

回答1:


Issue:

Whenever payload key is present in options/params argument of UrlFetchApp, the method is set to post by default. And any attempt to change the method to get is "silently" ignored. Other similar scripting platforms automatically convert the payload to url query parameters. But, UrlFetchApp silently changes the method to post and nothing else.

Solution:

Re-create the data object as a query string. For example, data:{x:1,y:2} should be changed to ?x=1&y=2 and appended to url.

Snippet:

function testapi() {
  var data = {
    resource_id: '1b702208-44bf-4829-b620-4615ee19b57c', // the resource id
    limit: 5, // get 5 results
    q: 'Yishun', // query for 'jones'
  };

  var options = {
    method: 'get',
    // 'contentType': 'application/json',
    // 'payload' : data,//If set, method is ignored.
    headers: { Accept: '*/*', 'Content-Type': 'application/json' },
    muteHttpExceptions: true,
  };

  var url = 'https://data.gov.sg/api/action/datastore_search';
  //var url = 'https://httpbin.org/get'; test the method
  var query = '?',
    i = 0;
  for (var key in data) {
    i ? (query += '&') : null;
    query += key + '=' + data[key];
    i = 1;
  }
  url += query;
  var response = UrlFetchApp.fetch(url, options).getContentText();
  Logger.log(response);
}

Related:

UrlSearchParams



来源:https://stackoverflow.com/questions/56200001/jsonp-ajax-in-google-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!