Adding New Record to an app via web form with jQuery and ajax

I’m a bit of a novice with jQuery and ajax when it comes to sending data in json format to a REST API.  I have created a form and the underlying js to process the form fields into the correct json format.  However when I try to post the body and the header to the REST API I get the following errors in the console:

OPTIONS https://mywebspace.kintone.com/k/v1/record.json 405 (JSPs only permit GET POST or HEAD)

Failed to load https://mywebspace.kintone.com/k/v1/record.json: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘https://mywebspace.com’ is therefore not allowed access. The response had HTTP status code 405.

 

Here is my js for the form and ajax:

$(document).ready(function() {

alert(‘jQuery is up and running!’)

$.fn.serializeObject=function()

{

//Array to hold field object data

varobjFieldLabel= {};

//Array to hold body object data that will be sent in a json body,

//this will contain field and value data via nested arrays

varobjBody= {};

//Set the application ID for Kintone json body

objBody[‘app’] =6;

//Serialize form data into an array

varobjArray=this.serializeArray();

//Loop through the form data

$.each(objArray, function() {

//Array to hold value data that will be nested in the field label array

varobjValue= {};

if (objFieldLabel[this.name] !==undefined) {

if (!objFieldLabel[this.name].push) {

objFieldLabel[this.name] = [objFieldLabel[this.name]];

}

objValue[‘value’] =this.value||’’;

objFieldLabel[this.name].push(objValue);

}

else {

objValue[‘value’] =this.value||’’;

objFieldLabel[this.name] =objValue;

}

});

objBody[‘record’] =objFieldLabel;

returnobjBody;

};

$(’#your-form’).on(‘submit’, function(e) {

e.preventDefault();

varjsonRecordToPost=JSON.stringify($(‘form’).serializeObject());

 

$.ajax({

url:‘https://mywebspace.kintone.com/k/v1/record.json’,

// beforeSend: function(xhr) {

// xhr.setRequestHeader(“X-Cybozu-API-Token”,“supersecretcodestring”),

// xhr.setRequestHeader(“X-HTTP-Method-Override”,“POST”),

// xhr.setRequestHeader(“Content-Type”,“application/json”);

// },

headers : {

‘X-Cybozu-API-Token’:‘supersecretcodestring’,

‘Content-Type’ :‘application/json’,

‘X-HTTP-Method-Override’ :‘POST’

},

contentType :‘application/json’,

type:‘POST’,

datatype:‘json’,

processData:false,

data:jsonRecordToPost,

success:function(data){

console.log(jsonRecordToPost);

$(’#result’).text(jsonRecordToPost);

// console.log(json);

},

error:function(data){

console.log(data);

}

})

.done(function(response){

$(’#result’).text(response.message);

});

});

});

Generally, webpages can’t access to other pages or APIs while you use JavaScript. If you don’t know that, please check this discussion.

https://developer.kintone.io/hc/en-us/community/posts/115008083388/comments/115003863707

So in order to submit data to the REST API on kintone I need to use a proxy for the API that is part of the allowed “Access-Control-Allow-Origin”.  Is there a list of API proxies that are allowed to interface with Kintone?

There is no public proxies and no list. If you want to access with JavaScript, I guess you have to prepare the corresponding functions as mentioned in the previous article.

Thanks Ryu,

Translate on the page is a bit of a mess but I get the general idea.  I have to create my own Web API and link to Lamdba then to kintone.  So just to be straight the restriction to submit data via an html page is limited to the JavaScript library.  So if I were to use PHP with cURL or Python I would be able to send the data then? 

If you do not mind javascript, you can also access kintone using PHP, Python or any other languages. And some libraries are available as follows.

 

PHP https://github.com/ochi51/cybozu-http

Python https://github.com/icoxfog417/pykintone

Ruby https://github.com/jue58/kintone

.NET https://github.com/icoxfog417/kintoneDotNET

Java https://github.com/kintone/java-sdk

 

In the end used PHP curl to submit the JSON data to Kintone to add records.  Thanks for the help.

Great works! Thank you for your polite report.

Hi Scott Shadler,

I am trying to PHP curl as well but did not have success. Can you please post your code here if you still have it. How you connected to Kintone. Did you use API Token or OAuth client? If you can give any help on this, that is great.Here is my code snippet, which is failing saying the Redirect URI is invalid

<?php
If(!isset($_GET[‘code’]))
{
//Initialize a CURL session
$ch=curl_init();

//From URL to get webpage contents
$url=“https://{sudomain_name}.kintone.com/oauth2/authorization?client_id={Client_ID}&redirect_uri=http%3A%2F%2Flocalhost%2Fcurlget.php&state=state1&response_type=code&scope=k%3Aapp_record%3Aread”;

curl_setopt($ch, CURLOPT_POST, 1);

//grab URL and pass it to the variable
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

//Execute and get result
$result = curl_exec($ch);
if(!$result)
{
die(“Connection Failure”);
}
else
{
echo $result;
}
curl_close($ch);
}
else
{
echo $_GET[‘code’];
}