Post values to cakePHP controller action and display it - cakephp

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).

Related

Java, Struts 1.2, Json Angular JS - How can I get json response from action class to my Angular JS page?

I need a clarification on this below technology
Current technology:
Struts 1.2
Jsp
Java
AngularJS
Currently I am trying to migrate one of my jsp page to AngularJS page which is client req.
I am trying using below code to get json response, but I unable to get the json data in my jsp page.
Question:
1) How to get a json object from the response of action class in struts?
2) Currently :
Note: one of temporary way I able to get json object in jsp using session.setAttribute() and getAttribute.. but its not rite way to do it as I just getting all my table data and put in session. apart from this any way please help..
Code :
In jsp:
$scope.getDataFromServer = function() {
$http({
method : 'GET',
url : '/com/test/Browse.do'
}).then (function successCallback(response) {
var itemsDetails = JSON.parse(response);
$scope.person = response;
}).error(function(data, status, headers, config) {
});
};
**//The above response getting as object unable to get json object.**
In action class:
public void doExecute(ActionContext ctx) throws IOException,
ServletException {
PersonData personData = new PersonData();//[PersonData class available]
personData.setFirstName("Mohaideen");
personData.setLastName("Jamil");
String json = new Gson().toJson(personData);
ctx.forwardToInput();
}
I able to get the data using session attribute like below, but it is not rite way will make me to avoid struts action and also whole table data putting in session I not sure whether it will rite way or secure way.
var data1 = '<%=session.getAttribute("jsonobjtest")%>';
Can please help me out to get the rite way to implement with struts 1.2 and angularjs? if we cannot do it help me guide me the workaround how to achieve with angularjs?
Hie there from my understanding you want to get a json object from your Java backend
here is how i do http requests in AngularJs 1.5.11
(function () {
'use strict';
angular.module("myapp")
.controller("HelloController", function($scope,$http){
$scope.getDataFromServer = function() {
$http.get("/com/test/Browse.do").success(function(response,status){
//console.log(response,status); check your data
$scope.person = JSON.parse(response);
});
}
});

How to send data from angularjs to cakephp

I have all my actions done in cakephp. I was using a platform in client side, and now I decided to migrate to Ionic/angularjs, but as I saw, they do not "talk" with each other. Angular uses Content-Type: application/json that cakePHP does not read with request->data or request->query.
I found some workarounds that I need to change all the actions in my server side. Is there another way to keep it without changes? I´d like to keep it working in the old client version too.
[Edited]
In algularjs I´m using $http service.
I'm not sure that I understand your problem.
If you send json data to your server with a post request, you can get data with a cakephp application easily:
jsonData = {data: [{key: 'a', value: 'b'}, {key: 'a', value: 'b'}]};
var req = {
method: 'POST',
url: 'http://example.com/Tests',
data: jsonData
}
$http(req);
Then in your controller you can get data:
class MyController extends AppController{
// ...
public function action(){
debug($this->request->data);
// you have your data from your angularjs app
// ['data' => [['key' => 'a', value => 'b'], ['key' => 'a', value => 'b']]];
}
I solved the problem changing the $http header to "application/x-www-form-urlencoded", I saw this post
body = 'cel=xxxx&pass=zczx';
var req = {
method: 'POST',
url: 'http://localhost/cakefacility/usuarios/checkUsuario.json',
data: body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
$http(req);
For Sending Data from angular to cakephp project you need to create REST API at cakephp project for creating REST API you can follow my forked github repo.
After Creating API in cakephp project you just call API like:
For Call API by GET:
$http.get("<REST API URL>").then(function(response) {
......
......
});
For Call API by POST:
$http.post(<REST API URL>, <DATA TO SEND>).success(function(data, status) {
.......
.......
});

Retrieve data from a JsonP proxy in sencha touch

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.

Web2py and Sencha

I'm new to Web2py and Sencha, and I would like to see a simple example using both frameworks. I have googled but I haven't found anything.
Many thanks for your help.
Finally I got everything working. To ensure that the js files are rendered it is necessary to paste them in the static folder of the web2py project. With Sencha Architect I have created a project in the mentioned location, so for example to call the controller from the js view an Ext.Ajax.request is made:
onDataRender: function(component, eOpts) {
Ext.Ajax.request
({
url: '/r/rec/getdata',
method: 'GET',
params: '',
success: function(response)
{
o=Ext.decode(response.responseText);
component.setSource(o);
console.log(response.responseText);
},
failure: function(response)
{
component.setSource({"Error" : "No data"});
console.log(response.responseText);
}
});
},
The controller then gets the resquested data from database, generates a json and returns it to the view layer:
def getdata():
jsondata="{"
data=db.song.find()
for s in data:
jsondata+="\""+str(s["_id"])+"\" : \""+str(s["name"]).replace("\"","")+"\","
return jsondata[:-1]+"}"
The getdata method gets all the data (it is a test example), to get a specific record the id can be passed as a parameter with request.args(0).

Cakephp ajax request invalid url

i have a view page in which i have to load different views according to the option selected in a selecbox. But my problem is that the url to which ajax request is sent is not correct.
The correct path to be formatted is like this http://pc12/cakephp/users/getView but the ajax request goes to http://pc12/users/getview. What is my problem here?? My code is below:
jQuery('#ptype').change(function(){
var param = "id="+jQuery(this).val();
jQuery.ajax({
type: "POST",
url: "/users/getView",
data: param,
dataType: "text",
success: function(data){
if(data) jQuery('#profile_info').html(data); }
});
});
write complete address:
/AppName/Controller/Action/
you can use firebug for debug any ajax requests. it's very helpful.
Problem is the first front slash as I think.
url: "**/**users/getView",
in url: remove the first front slash(/) before users and it will work fine. I am using the same format without any problem. It will be like.
url: "users/getView",
it is easy and clear to use than your replacement: Html->url(array('controller' => 'users', 'action' => 'getView')); ?>

Resources