I'm trying to use the 'rest' proxy with extjs to connect to my delphi xe2 rest server.
This does a request and works when I use jsonp
proxy: {
type: 'jsonp',
api: {
create: {url:"http://10.0.29.64:8080/datasnap/rest/tsmbank/client", method:"PUT"},
read: "http://10.0.29.64:8080/datasnap/rest/tsmbank/client",
update: {url:"http://10.0.29.64:8080/datasnap/rest/tsmbank/client", method:"POST"},
destroy: {url:"http://10.0.29.64:8080/datasnap/rest/tsmbank/Client", method:"DELETE"}
},
callbackKey: 'theCallbackFunction',
reader: {
type: 'json',
root: 'allclients',
totalProperty: 'totalCount'
},
writer: new Ext.data.JsonWriter({
//type: 'json',
writeAllFields: true
})
}
But if I try to use the Restproxy type it sends an "OPTIONS" method
proxy: {
type: 'rest',
url: "http://10.0.29.64:8080/datasnap/rest/tsmbank/client",
callbackKey: 'theCallbackFunction',
reader: {
type: 'json',
root: 'allclients',
totalProperty: 'totalCount'
},
writer: new Ext.data.JsonWriter({
//type: 'json',
writeAllFields:true
})
}
Or I guess the other quetions is how to handle the "OPTIONS" request method with a delphi rest server?
Related
I need to make this request but in axios
$.ajax({
type: "POST",
url: url,
data: {'HTTP_CONTENT_LANGUAGE': 'en'},
xhrFields: {
withCredentials: true
},
I tried
params = {
data: {
'HTTP_CONTENT_LANGUAGE': 'en',
},
withCredentials: true
};
axios.post(url, params)
But didn't work what do I do?
Google Analytics v4 API now uses POST requests instead of GET request. And there are no good javascript examples out there yet for me to follow. I'm getting empty object Object { }, but I'm sure that data is there and ViewID is correct!
Any advice on what I am doing wrong? or are there any fully working example that I can follow? Thanks.
requestData = function () {
var url = "https://analyticsreporting.googleapis.com/v4/reports:batchGet?";
var params = {
"reportRequests":[{
"viewId":"12345678",
"dateRanges":[{
"startDate":"yesterday",
"endDate":"today"
}],
"metrics":[{
"expression":"ga:users"
}],
"dimensions": [{
"name":"ga:pagePath"
}]
}]
}
$.ajax({
url: url,
type: "POST",
data: params,
dataType: "json",
success: function(results) {
console.log(results)
},
error: function(xhr, ajaxOptions, thrownError) {
alert('failed');
alert(xhr.status);
alert(thrownError);
}
});
I would highly recommend you use the Google Javascript Client Library to simplify your life greatly. There are plenty of Code Samples using said library:
var DISCOVERY = 'https://analyticsreporting.googleapis.com/$discovery/rest';
// Load the API from the client discovery URL.
gapi.client.load(DISCOVERY).then(function() {
// Call the Analytics Reporting API V4 batchGet method.
gapi.client.analyticsreporting.reports.batchGet( {
"reportRequests":[{
"viewId":"12345678",
"dateRanges":[{ "startDate":"7daysAgo", "endDate":"today"}],
"metrics":[{"expression":"ga:users"}],
"dimensions": [{"name":"ga:pagePath"}]
}]
}).then(function(response) {
var formattedJson = JSON.stringify(response.result, null, 2);
document.getElementById('query-output').value = formattedJson;
}).then(null, function(err) {
// Log any errors.
console.log(err);
});
As for getting jQuery to work, a similar question was asked about nodejs Their solution was to set the content-type=application/json which fortunatly has been Asked and answered as well.
var url = "https://analyticsreporting.googleapis.com/v4/reports:batchGet?";
var data = {
"reportRequests":[{
"viewId":"12345678",
"dateRanges":[{ "startDate":"7daysAgo", "endDate":"today"}],
"metrics":[{"expression":"ga:users"}],
"dimensions": [{"name":"ga:pagePath"}]
}]
}
$.ajax({
url: url,
type: "POST",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(results) {
console.log(results)
},
error: function(xhr, ajaxOptions, thrownError) {
alert('failed');
alert(xhr.status);
alert(thrownError);
}
});
I am using Rest proxy in Extjs Model as:
Ext.define('ThemeApp.model.peopleModel', {
extend: 'Ext.data.Model', fields: [
{ name: 'userId' },
{ name: 'title' },
{ name: 'body'}
],
proxy: {
type: 'rest',
format: 'json',
limitParam:"",
filterParam: "",
startParam:'',
pageParam:'',
url:'http://jsonplaceholder.typicode.com/posts/1',
api: {
read : 'http://jsonplaceholder.typicode.com/posts/1',
create: 'http://httpbin.org/post'},
headers: {'Content-Type': "application/json" },
reader: {
type: 'json',
//rootProperty:'issues'
},
writer: {
type: 'json'
}
In my view I am calling create function as:
var user = Ext.create('posts', {"userId": 124,"title": "sunt","body": "quia"});
user.save();
As I am testing everything on http://jsonplaceholder.typicode.com/ so I am expecting that code will work cause when I test GET and POST functionality via Postman utility everything works fine.
Can anyone point out my error?
I found my mistake.
In the following code I was not setting the correct name of my model, as it won't be "Posts"
var user = Ext.create('posts', {"userId": 124,"title": "sunt","body": "quia"});
user.save();
Also if you are trying with http://jsonplaceholder.typicode.com/ you are not supposed to send ID in the post request.
I'm new to Sencha Touch and I'm having problems to delete a record.
This is my model:
Ext.define('MyApp.model.Customer', {
extend: 'Ext.data.Model',
...
config: {
idProperty: 'key',
fields: [
{
name: 'key',
type: 'auto'
},
{
name: 'name',
type: 'string'
}
...
]
}
....
}
and this is an event associated with a delete customer button:
onDeleteCustomerBtnTap: function(button, e, eOpts) {
var data = this.getCustomerDetail().getData();
var store = Ext.getStore("CustomerStore");
var record = store.getById(data.key);
store.remove(record);
store.sync();
}
EDIT:
this is the store
proxy: {
type: 'rest',
url: 'http://example.com/customers',
useDefaultXhrHeader: false,
appendId: false,
reader: {
type: 'json',
idProperty: 'key',
rootProperty: 'data'
},
writer: {
type: 'json'
}
}
The problem is when it tries to sync the store. The request url is http://example.com/customers?_dc=1394840324234 instead of http://example.com/customers/10?_dc=1394840324234.
What am I doing wrong?
I finally used the erase method of Ext.data.Model. Here is my final version:
var data = this.getCustomerDetail().getData();
var store = Ext.getStore("CustomerStore");
var record = store.getById(data.key);
record.erase({
success: function() {
// ...
}
}
});
are you defining your proxy as 'rest' and not 'ajax'?
proxy: {
type: 'rest',
url : '/users'
}
I am trying to access a REST service using extjs proxy rest, but the url that is being sent looks weird, take a look:
/rest/v1/distribution-list/1*?id=1*
I dont know why 'id' is being sent.
It shoul send '/rest/v1/distribution-list/1'
Any ideas?
this is my model
Ext.define('Wave.model.DistributionList', {
extend: 'Ext.data.Model',
fields: [
{name: 'id'},
{name: 'name', type: 'string'},
{name: 'status', type: 'string'}
],
proxy: {
type: 'rest',
noCache: false,
reader: {
type: 'json'
},
writer: {
type: 'json'
},
actionMethods: {
create: 'POST',
read: 'GET', // defaults to GET
update: 'POST',
destroy: 'DELETE'
},
api: {
read: '/rest/v1/distribution-list/',
create: '/rest/v1/distribution-list/',
update: '/rest/v1/distribution-list/',
destroy: '/rest/v1/distribution-list/'
}
}
});
Cheers
-Henrique
the ID have been send is setted by Extjs.you can change that using the idParam to changed to other one;
Working with sencha-touch 2.3.1 and rest proxy, ExtJS creates the URL to the action methods with query string parameters, like you said: /rest/v1/distribution-list/?id=1.
If you don't want to append the id, you can change appendId to false inside proxy config.