-
Notifications
You must be signed in to change notification settings - Fork 5
DataAPI SDK english MT.DataAPI Endpoint methods overview
Taku AMANO edited this page Jul 5, 2013
·
1 revision
The :site_id
is called a method parameter for the /:site_id/entries
endpoint.
You can pass a method parameter by various format.
var siteId = 1;
api.listEntries(siteId, function(response) {
// Do stuff
});
var site = {
id: 1
};
api.listEntries(site, function(response) {
// Do stuff
});
var siteId = function() { return 1 };
api.listEntries(siteId, function(response) {
// Do stuff
});
var site = {
id: function() { return 1 }
};
api.listEntries(site, function(response) {
// Do stuff
});
You can pass query parameters and request body by various format.
var query = 'search=QUERY';
api.listEntries(site, query, function(response) {
// Do stuff
});
var query = {
search: 'QUERY'
};
api.listEntries(site, query, function(response) {
// Do stuff
});
<form id="search-form">
<input type="text" name="search" />
</form>
<script type="text/javascript">
var query = document.getElementById('search-form');
api.listEntries(site, query, function(response) {
// Do stuff
});
</script>
// This code requires that your browser had implemented FormData.
var query = new FormData();
query.append('search', 'query');
api.listEntries(site, query, function(response) {
// Do stuff
});
You can pass a resource to create or update by various format.
// The resource data does not require strictness.
// Requires only values you want to create or to update.
// Allows including Unnecessary value.
var data = {
title: 'TITLE',
body: 'BODY'
};
api.updateEntry(site, entryId, data, function(response) {
// Do stuff
});
Or, you can simplly pass the data that had been retrieved from API.
api.getEntry(site, entryId, function(entry) {
entry.title = 'UPDATED TITLE';
api.updateEntry(site, entryId, entry, function(response) {
// Do stuff
});
});
<form id="entry-form">
<input type="text" name="title" />
<textarea name="body" />
</form>
<script type="text/javascript">
var data = document.getElementById('entry-form');
api.createEntry(site, data, function(response) {
// Do stuff
});
</script>
You can pass a file by various format.
However, you must not forget that in every way, you can not open file selection using JavaScript in IE. The following code makes a security error in IE.
<form>
<input type="file" name="file" id="file" />
</form>
<a href="#" id="select-file">Select file</a>
<script type="text/javascript">
document.getElementById("select-file").onclick = function() {
getElementById("file").click();
return false;
};
var data = {
file: getElementById("file").files[0],
};
api.uploadAsset(siteId, data, function(response) {
if (response.error) {
// Handle error
return;
}
// Do stuff
});
</script>
You should rewrite these cases as follows.
<form>
<input type="file" name="file" id="file" />
</form>
<label for="file">Select file</label>
<script type="text/javascript">
var data = {
file: getElementById("file").files[0],
};
api.uploadAsset(siteId, data, function(response) {
if (response.error) {
// Handle error
return;
}
// Do stuff
});
</script>
var data = {
file: getElementById('file-input').files[0]
};
api.uploadAsset(siteId, data, function(response) {
// Do stuff
});
var data = {
file: getElementById('file-input')
};
api.uploadAsset(siteId, data, function(response) {
// Do stuff
});
<form id="asset-form">
<input type="file" name="file" />
</form>
<script type="text/javascript">
var data = document.getElementById('asset-form');
api.createEntry(site, data, function(response) {
// Do stuff
});
</script>
api.createEntry(site, data, function(response) {
console.log(response); // Response data
});
api.createEntry(site, data, function(response) {
if (response.error) {
console.log(response.error.code); // The HTTP response code
console.log(response.error.message); // The error message
console.log(response.error.data); // If current error has some optional data
}
});