Extjs Store Getting Data from Post - extjs

Good Morning,
I have a search endpoint that I that works when I call it like this:
Ext.Ajax.request({
url: '/Search/False',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
params: 'Attribute:ClosedDT;Value:2014-12-16',
success: function(conn, response, options, eOpts) {
alert(conn.responseText);
},
failure: function(conn, response, options, eOpts) {
alert(conn.responseText);
}
});
I want to use a proxy to load it into a store directly. After much googling I have tried this and i get back a POST /Search/False?_dc=1418738135737 net::ERR_EMPTY_RESPONSE
See current code below:
var proxyDefinition = {
type : 'rest',
api : {
read : '/Search/False'
},
actionMethods : {
read : 'POST'
},
reader : {
type : 'json'
},
paramsAsJson:true
};
returnValue = Ext.create('Ext.data.Store', {
model: 'Mdl1',
proxy: proxyDefinition
});
returnValue.load({params: 'Attribute:ClosedDT;Value:2014-12-16'});

The params config needs to be an object, not a string. Extjs will encode it for you because of paramsAsJson: true.
You should use:
params: {
Attribute: 'CloseDT',
Value: '204-12-16'
}

Related

ExtJS Ajax Request Method GET

I need to send a request via GET but ExtJS changes the method for OPTIONS
I changed useDefaultXhrHeader even though I could not send, I also need to disable CORS
https://fiddle.sencha.com/#view/editor
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.Ajax.request({
url: 'http://localhost:8888/api/sign/certificates',
method: "GET",
useDefaultXhrHeader : false,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
success: function (response, opts) {
var obj = Ext.decode(response.responseText);
console.dir(obj);
},
failure: function (response, opts) {
console.log('server-side failure with status code ' + response.status);
}
});
}
});
PRINT
I think you want to eliminate the _dc cache parameter, you can do it like this
noCache: false,
Use this in your Ext.Ajax.request({})
Hope this helps!!

asp.net 5 / mvc 6 / model binder / angular

I do not know because when I do a post by angular, objects are not populated, such as categories or status. Just the product.
However, note that the Request.Form list, the information is there.
The binder is not performed correctly.
What am I doing wrong?
Is it any web api configuration?
I've tried sending data via application/json, [frombody] ... I'm out of options.
Thanks in advance.
var product = {
id: 1,
name: "Name",
categories: [
{ id: 1, name: "name 1" },
{ id: 2, name: "name 2" }
],
status: { id: 1, name: "active" }
};
var config: ng.IRequestConfig;
config = { url: "", method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' } };
self.$http.post("api/produto", $.param(product), config)
.success(function () {
alert("OK");
});
[HttpPost]
public ProductInfo Post(ProductInfo item)
{
return item;
}
model image
request image
try this:
$http({
url: "api/produto",
method: "POST",
data: product,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.then(function(response) {
// success
},
function(response) { // optional
// failed
});
Or you could use the shortcut method:
$http.post('api/produto', data).then(successCallback, errorCallback);
Here's what I would try
var product = {
id: 1,
name: "Name",
categories: [
{ id: 1, name: "name 1" },
{ id: 2, name: "name 2" }
],
status: { id: 1, name: "active" }
};
var config: ng.IRequestConfig;
config = { url: "", method: "POST", headers: { 'Content-Type': 'application/json;charset=utf-8;' } };
self.$http.post("api/produto", product, config)
.success(function () {
alert("OK");
});
What is different is that I send the actual JavaScript object and it will be serialized as JSON. Web API will be able to deserialize the JSON into a pure C# object.
Trying to match key/value pair is normally just madness on complex objects.
There was no way to do it.
Either I create a custom model binder, or I'll have the new web api 6 mvc always use the frombody and force the ship by json.
At least that's what I did with my tests.
It worked just sending json and using frombody.
The documentation, really changed bind the web api 2 for this new integrated model to MVC.

object is null in angularjs using $resource

In my resources i have following code
app.factory('myResource', function ($resource, $http) {
var resource = $resource('http://localhost:1234/api/:pg/:obj', {
pg: '#pg',
obj: '#obj'
},
{
'put': {
method: 'PUT'
},
'get': { method: 'GET' },
'save': { method: 'POST' },
'query': { method: 'GET', isArray: true },
'remove': { method: 'DELETE' },
'delete': { method: 'DELETE' }
}
);
return resource;
});
and in controller i use like
var param = { pg: 'MyTask', obj: taskModel }
$scope.TaskId = myResource.put(param);
and on debugging data is fine in obj but getting null in model
public HttpResponseMessage put(MyTask model)
{}
Your resource is configured to serialize #pg and #obj into URI.
$resource('http://localhost:1234/api/:pg/:obj', {
But WEBAPI sees MyTask object(complex object) and expects it to be from request body.
Either change client side to include "obj" data into request body or change server-side to get model from uri using webapi attributes. It all depends on complexity of "obj" information.

Using $resource with Angular

I have a service that I am calling to get a collection which works fine. Here is the service:
angular.module('clinicalApp').factory('encounterService', function ($resource, $rootScope) {
var EncounterService = $resource('http://localhost:port/v2/encounters/:encounterId', {encounterId:'#id', port: ':8280'}, {
search: {
method: 'GET',
headers: {
'User': 'testuser',
'Content-Type': 'application/json'
}
},
save: {
headers: {
'User': 'testuser',
'Content-Type': 'application/json'
}
}
});
return EncounterService;
});
As I said, I am able to get the collection, but when I try to update a one of the elements in my collection, it is calling out to the wrong url. Here is how I am trying save my resource:
encounterService.save({
id: scope.encounter.id
});
And here is the url that is being hit:
http://localhost:8280/v2/encounters?id=12345
So it is appending the url as if it is a search parameter. How do I get it to append the id to the url like
encounters/12345
as it should?
Try this...
{encounterId:'#id', port: ':8280'}
Change To:
{encounterId:'#encounterId', port: ':8280'}
AND
encounterService.save({
id: scope.encounter.id
});
Change To:
encounterService.save({
encounterId: scope.encounter.id
});

Move Ajax respose data to store in Sencha touch

how to add or save data that getting from ajax request to store or to model sencha touch 2
I have controller, store and a model. Ext.Ajax.request(); is called from controller and when it was successful I want move that data to store in json format
Ext.define('Myapp.controller.HomeController', {
extend: 'Ext.app.Controller',
config: {
control: {
"#homepage_id": {
show: 'onHomepage_idShow'
}
}
},
onHomepage_idShow: function (component, eOpts) {
var token = localStorage.getItem('Token'); //**************************************
console.log('test home', token);
var customHeaders = {
'Content-Type': 'application/json; charset=utf-8',
'ApiAuth': token
};
this.callAjax(customHeaders);
},
callAjax: function (headers) {
var customHeaders = headers;
Ext.Ajax.request({
url: 'http://localhost:9098/Folder/json/Get',
params: Ext.util.JSON.encode({
folderId: 0
}),
method: 'POST',
headers: customHeaders,
success: function (response) {
var decode_text = Ext.decode(response.responseText);
/*I want to add decode_text to a store from this contoller..*/
//var storez = Ext.data.StoreManager.lookup('commomStore_id');//****************
//this.getDataList().setStore(storez);
console.log(storez);
// process server response here
},
failure: function (response, opts) {
Ext.Msg.alert('Error', 'Error while submitting the form');
console.log(response.responseText);
},
scope: this
});
My Store:
Ext.define('Myapp.store.CommonStore', {
extend: 'Ext.data.Store',
requires: [
'Myapp.model.AuthTokenmodel'],
config: {
autoLoad: true,
model: 'Myapp.model.AuthTokenmodel',
storeId: 'commonStote_id',
proxy: {
type: 'localstorage',
id: 'commomStore_id',
reader: {
type: 'json'
}
},
fields: [{
name: 'authtoken'
}]
}
});
For that you have to parse your response and create Myapp.model.AuthTokenmodel objects out of it and then add those objects to your store using add method.
BTW if your response is in JSOn format you should parse it to JSON instead of text like this:
var respObj = Ext.JSON.decode(response.responseText);
console.log(respObj);
then create model objects using respObj data and add those to store:
var storez = Ext.data.StoreManager.lookup('commomStore_id');
storez.add(Ext.create('Myapp.model.AuthTokenmodel', {authtoken : respObj.authToken}));
Ext.getStore('commomStore_id').loadData(decode_text);

Resources