Getting kintone app records data from other domain by RestAPI

3 followers Follow
Avatar
Salman Quader

I'm new in kintone. in kintone subdomain, everything are working properly. But When i try to get records data from other domain using ajax request then it returns an error like,

return error----
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at https://atom.kintone.com/k/v1/record.json?app=1&id=1.
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

my code example:

 var params = '?app=1&id=1';
var url = 'https://atom.kintone.com/k/v1/record.json' + params;

var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('X-Cybozu-API-Token', 'api-token-string');

xhr.onload = function() {
if (xhr.status === 200) {
// success
console.log(JSON.parse(xhr.responseText));
} else {
// error
console.log(JSON.parse(xhr.responseText));
}
};
xhr.send();

But when i try in postman then it works properly.

Again, I used kintone.proxy() it also doesn't work. it returns another error like,

return error---- ReferenceError: kintone is not defined

my code example is bellow,

var url = 'https://atom.kintone.com/k/v1/records.json';
var method = 'POST';
var headers = {
'Content-Type':'application/json',
'X-Cybozu-API-Token':'api-token-string',
'X-HTTP-Method-Override':'GET'
};
var params = {
app:1,
id:1
};

kintone.proxy(url, method, headers, params).then(function(r){
console.log(r);
}).catch(function(e){
console.log(e);
});

Please know me, How to get records data using RestAPI from other cross domain.

0

3 comments

Avatar
Junko Werner 1 vote
Hi Salman,
 
XML request cannot be executed because of cross domain restrictions.
 
"Due to cross domain restrictions, communication between kintone.com and external sites using XHR(XMLHttpReuest) cannot be run. The Access-Control-Allow-Origin header cannot be added."
 
 
As an alternative option, kinton.proxy can be used.
 
Here is an example of code based yours.
 
  var url = 'https://<subdomain>.kintone.com/k/v1/record.json';
         var method = 'POST';
         var headers = {
         'Content-Type':'application/json',
         'X-Cybozu-API-Token':'<API token>'
         };

         var params = {
         app:<app ID>
         };       

        kintone.proxy(url, method, headers, params).then(function(r){
         console.log(JSON.parse(r));
         }).catch(function(e){
         console.log(e);
         });
"'X-HTTP-Method-Override':'GET'" is removed in this above example.
 
In your code, "records.json" has been registered multiple times, but in this case, the required parameters need to be specified, so only one time is registered in the above example.
 
 
Thanks.
 
Junko
1

Please sign in to leave a comment.