I am trying to get the data from a JSONP proxy in sencha touch 2.3(I am using sencha architect 3 to develop). I was successfully able to place jsonp call and get the data back. But I am not getting how to separate every single element of json response. Here is my json Response:-
{"data":[{ "PLANTNUM": "1557", "ROUTEID": "90625", "DELIVERYDATE": "2014-02-12T00:00:00-06:00", "MESCOD": "15", "MESFCT": "DLV", "ORIGID": "HH", "JMSTIME": "02/11/2014 00:11:21", }],"success" : true}
Here is my function
success: function(response){
console.log(response);
var temp=response.data.PLANTNUM;
console.log(temp);
}
I can see below in my console:-
Here is my jsonP request
Ext.data.JsonP.request({
url: 'http://localhost:12608',
callbackKey: 'cbfn',
params: {
method: 'sapout',
type: 'sap',
ordnum: '1034986850'
}
I tried using response.PLANTNUM but that is also not working. It always shows undefined
Can anyone help me out here.
Thanks
data is an array, so you want response.data[0].PLATINUM.
Related
I am facing a very strange problem today with Angular, I have an api call whose response is giving (this is what i got from raw api call in new tab)
{ "startTime" : 1524021720000, "endTime" : 1524022800000 }
but after making an http call and storing response in $scope.dataSet variable this is what i am getting,
{ "startTime" : 1524001920000, "endTime" : 1524003000000 }
Below is the code snippet:
$http({url: testDetailsUrl, method: 'GET'})
.then(function(response) {
$scope.dataset = response.data;
console.log($scope.dataset.startTime, $scope.dataset.endTime);
}
I wanted to filter out value through angular pipe to show it as a time string, but i got strange results bucause of this change in variable values. Can anybody please explain me what happened here?
PS: I am using angular 1.4.9 and my browser's timezone is GMT+5:30..
According to me it is nothing to do with your browser's timezone GMT+5:30. I even faced similar issue, where WEB API is giving me new value(xyz) but in the $scope previous value(abc) used to display in the front end.
To fix this issue you can use below code,
$http({url: testDetailsUrl, method: 'GET', cache: false, headers: {
'Cache-Control' : 'no-cache'
}})
I need to send video from disk to Cognitive Services Emotion. I have almost done, but I was not capable to figure out how to format body properly and I can´t use '{url: http://...}' because I can´t expose the videos I want to submit. My code:
$.ajax({
"headers":{
"Content-Type":"application/octet-stream",
"Ocp-Apim-Subscription-Key":"SECRET"
},
type: "POST",
url: url,
"data": JSON.stringify({video: data}),
success: (x,stat,res) => {
},
error: (res) => {
}
});
As you can see I´ve tried to use 'octet-stream' to send the video. And that is one of many ways I did it. I don´t know if I need to send a JSON(similar when you send url) or do something else. I couldn´t find anything about it on official documentation. Help??
Thanks!
Please take a look at Sending binary data in javascript over HTTP.
$.ajax({
"headers":{
"Content-Type":"application/octet-stream",
"Ocp-Apim-Subscription-Key":"SECRET"
},
type: "POST",
url: url,
data: data,
success: (x,stat,res) => {
},
error: (res) => {
}
});
You want the body payload to be the raw (unencoded) binary data for Cognitive Services. The accepted formats are listed here.
One other important thing to remember is that jQuery calls the error handler for HTTP 202 responses, which is what you get from this API. So your error handler needs to handle that case, or, more to the point, your success handler won't ever be called.
WORK AROUND IS AT THE BOTTOM
Original problem
There are question like this all over the web and none of them really have answer for me. I can't get an http PATCH operation to work using angular to save my life. I've implemented $http, with shortcut $http.patch and without using the config object method:PATCH. I've used $resource by adding a custom method. And I've implemented Restangular using their patch and I'm getting the same error. I have the correct Content-Type as suggested in other posts. I think it's safe to say at this point, it's something I'm missing. I'm getting the same "404" message via postman when trying to patch. I can PUT, GET, POST, and DELETE, but not PATCH.
In the following images you can see that the resource exists for GET. But when trying to patch I get 404. Browsing to that endpoint shows the record. Which is stored in Mongodb.
Here's some code snippets:
Resangular GET:
var corporiumRecord = Restangular.one('corporium-mgmnts', $scope.uuid);
corporiumRecord.get().then(function(res) {
console.log(res)
}, function(err) {
console.log('Restangular failed: ', err)
});
Restangular Patch:
var data = {
corporiumId: $scope.newBlock
};
var corporiumRecord = Restangular.one('corporium-mgmnts', $scope.uuid);
corporiumRecord.patch(data).then(function(res) {
console.log(res)
}, function(err) {
console.log('Restangular failed: ', err)
});
$http attempt using config object:
controller code:
httpCorporiumSrv.updateCorporiumId('/corporium-mgmnts/' + $scope.params.id, data)
.then(handleUpdateSuccess)
.catch(handleUpdateError);
service code, tried forcing the content-type header but got same result
with or without it:
function updateCorporiumId(url, data) {
return $http({
method: 'PATCH',
url: url,
data: angular.toJson(data),
headers: {
'Content-Type': 'application/json;charset=utf-8'
}
//transformRequest: transformUpdateData
})
.then(handleUpdateSuccess)
.catch(handleUpdateErrors);
}
Using the .patch shortcut:
function updateCorporiumId(url, data) {
return $http.patch(url, data, {
transformRequest: transformUpdateData
})
.then(handleUpdateSuccess)
.catch(handleUpdateErrors);
}
Thing is I've tried this every which way I know how. I don't even know how to start debugging any more. I'm just getting 404 on a resource that does exist. Any suggestions on what might be happening to my request would be great.
Resolution:
For anyone having this issue, if you could post the fix or what's going on here to this point or PM me that would be awesome I'd like to know. I ended up just using PUT to fix this.
Quick Restangular solution:
Build the url template for findByOne like function using Restangular.one(url, _id) where '_id', is the id of the resource you're looking for. .get() goes out and finds that one resource by said id, which you can populate dynamically however you like. Once you have the one resource with GET copy it with Restangular.copy() which is different from angular.copy as it doesn't bind 'this' to the new object. Change what needs to be changed or added in the new object and then perform a .put() on it.
var corporiumRecord = Restangular.one('corporium-mgmnts', $scope.uuid);
corporiumRecord.get().then(function(res) {
var update = Restangular.copy(res);
// update date corporiumId
update.corporiumId = $scope.newBlock;
// submit new doc with altered value
update.put().then(function() {
console.log('updated')
});
console.log(update)
}, function(err) {
console.log('Restangular failed: ', err)
});
Also because mongo uses _id and Restangular uses id you have to add this to your module
angular.module('corporium-mgmnts').config(function(RestangularProvider) {
RestangularProvider.setMethodOverriders(['put', 'patch']);
// setRestangularFields is required for mongodb
RestangularProvider.setRestangularFields({
id: "_id"
});
});
I'm a beginner. I've written a test application made of an AngularJs GUI on the client side and a PHP API on the server side.
This is the angular service handling the requests
myApp.factory('Book', ['$resource', 'API_URL', function($resource, API_URL){
return $resource(API_URL + '/books/:bookId', {bookId: '#bookId'}, {
get: { method: 'GET', isArray:true },
update: { method: 'PUT'},
save: { method: 'POST'},
delete: {method:'DELETE'},
});
}]);
When I submit a book from the Angular app I can catch the POST in Slim by using
$post_a = json_decode($app->request->getBody());
//$post_b = $app->request->post(); //this would be empty
When I use Postman and I perform a POST I can catch the POST in Slim by using
//$post_a = json_decode($app->request->getBody()); // this would be empty
$post_b = $app->request->post();
I don't get why there is this difference. Could you please explain?
Am I not meant to catch the post just with $app->request->post(); in both the cases? Why the post coming from Angular can be caught only with $app->request->getBody()?
The $app->request->post() method retrieves key/value data submitted in a application/x-www-form-urlencoded request. If the request uses a different content-type (e.g. application/json), you can retrieve the raw request body with the $app->request->getBody() method and decode it as necessary. Let me know if you have further questions.
You could still use
$post_b = $app->request->post()
in Slim.
As long as you call this REST service from html form (AngularJS) by passing the data as form value formatted instead of as JSON.
If in AngularJS you have the data in JSON format, you have to translate it first into form. Below is the example how to invoke this REST service:
Object.toparams = function ObjecttoParams(obj) {
var p = [];
for (var key in obj) {
p.push(key + '=' + encodeURIComponent(obj[key]));
}
return p.join('&');
};
$http({
method: 'POST',
url: url,
data: Object.toparams(myobject),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
myobject is the data in JSON format that is going to be created
Thanks Josh..Your answers works for me.
Steps to follow:
1.You need to send request in json format under raw tab like this:
{"username":"admin","password":"admin"}
2.You need to set Content-Type to application/json in the headers.
That's it and it will work.
I am using Facebook Javascript API for Facebook authentication and implementing other Facebook features, in a cake PHP based site. Now I am using API for fetching the Facebook friends and I need to do some operations with the friends list. So I am posting the JSON object array to the corresponding controller action. Later on this page is loaded using another AJAX call . In between I am loosing the posted data. What I need is, I need to compare frien's list with existing Facebook IDs. I am using the below code
FB.api('/me/friends', function(response) {
$.ajax({
type: 'post',
url: baseUrl+'/user_details/userlist',
data: response,
dataType: 'json',
success: function() {
}});
});
How can I achieve this ? or I need to use PHP based SDK ?
Change your data option to this format:
data: { "data" : response },
Then check $this->request->data on your Cake controller (assuming Cake 2.x).