CakePHP - Empty $this->request->data array after Ajax POST - cakephp

I'm trying to save data which comes from an AJAX POST request in JSON but
There is empty $this->request->data in controller.
My AJAX call:
$.ajax({
type: "POST",
url: 'localhost/pages/register',
dataType: 'json',
data: pass_data,
});
JSON data:
var email = $("#email").val();
var verifyemail = $("#verifyemail").val();
var password = $("#password").val();
var confirm_password = $("#vPassword").val();
var pass_data = {
'data[User][email]':email,
'data[User][verifyemail]':verifyemail,
'data[User][password]':password,
'data[User][confirm_password]':confirm_password,
}
I'm use CakePHP 2.3. and jQuery 1.7.
Do you have any ideas?

Related

angular POST not working with servlet

I am trying to use angularjs POST (or even GET) to a servlet with the following:
var json = { hello: "world" }
var deffered = $q.defer();
$http({
method: "POST",
url: url,
headers: { "Content-Type" : "application/json" },
request: JSON.stringify(json)
}).then(data) {
if(data.data) {
deferred.resolve({
response : data
});
)
})
return deffered.promise;
within the servlet, simple:
String val = request.getParameter("request")
it never seems to see it
I have tried:
data: JSON.stringify({ request: json })
data: { request: json }
"request" : JSON.stringify(json)
etc
if I comment out the getParameter and just return a generic value using Gson
JsonObject json = new JsonObject();
json.addProperty("This", "works");
response.getWriter().print(new Gson().toJson(json));
that comes back fine so is there something within the angular POST I am doing wrong here? I have also tried using "GET" instead but same result.
EDIT: I would like to understand POST method and the "proper" way to get the data from the json object if getParameter is wrong please~
getParameter() returns http request parameters, you should add this params by using :
params: JSON.stringify(json)
Not with
request: JSON.stringify(json)
Take a look in params in get and params in post.

how i can send post data array angular and get data on api laravel

i have code like it on angular
my array :
[{"kode":"123","nama":"satu dua tiga"},{"kode":"321","nama":"tiga dua satu"}]
$http({
method: 'POST',
url: 'api/insertCustomerArr',
data: myarray
}).then(function successCallback(response) {
}, function errorCallback(response) {
});
and how i can get this data on controller laravel and how i can looping ?
public function insertCustomerArr(Request $request)
{
echo count($request);
exit;
}
this code result count 1, how i can get data?
You can check using
return $request->all();
this will return all the data you posted from angular request
Laravel $request object has a request property inside of it.
So that basically $request->request->all(); should contain all the data that came from angular.

AngularJs POST request for json not working

Hi i tried POST json data in two ways its response in null
var jsonData = $scope.addedCat;
console.log(jsonData);
var request = $http({
method:"POST",
url:base_url+"Category_controller/json_test",
data: JSON.stringify(jsonData),
dataType: "application/json"
});
request.success(
function(response){
console.log(response);
});
var cat_j = $scope.addedCat;
var data = $.param({ json:JSON.stringify(cat_j)});
$http.post(base_url+"Category_controller/json_test/",data).success(function(data, status) {
console.log(data);
console.log(status);
})
How we decode the json data in php.
I tried like this in Codeignitor framework.
$cjson = $this->input->post('jsonData');
$cat_json = json_decode($cjson);
echo json_encode($cat_json);
On your server php file , try that instead, and you get the parametes passed from client:
//get parameters
$params = json_decode(file_get_contents('php://input'), true); //read values from angular directive
Superglobal $_post only support application/x-www-form-urlencoded and multipart/form-data-encoded.
For application/json you should use php://input which can give you the raw bytes of the data. Here is a sample code of how to get the input data:
// get the raw POST data
$rawData = file_get_contents("php://input");
// this returns null if not valid json
print_r(json_decode($rawData));
$data = json_decode(file_get_contents('php://input'), true);
and do $data['jsonData']
now this $data['jsonData'] === $this->input->post('jsonData');

Unable to POST data using collection's fetch method

From what I understand, I'm able to use my collection to fetch, POST data, to an api. In this case, I'm using the Bing translate api.
My fetch looks like this:
app.searches.fetch({
contentType: "application/x-www-form-urlencoded",
type: 'POST',
data: { client_id: 'test',client_secret:'test', scope:'http://api.microsofttranslator.com', grant_type: 'client_credentials'},
dataType: 'jsonp',
success: function () {
}
});
Everytime this fetch fires, it performs a GET, not a POST. Right now, my project is barren so I don't think anything is interfering. Have I written this fetch incorrectly?
EDIT:
here's the collection:
var app = app || {};
(function(){
'use strict';
// Searches Collection
//---------------------
var Searches = Backbone.Collection.extend({
//referebce to this collection's model
model: app.Search,
url: 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
});
app.searches = new Searches();
})();

cakephp find method not returning object even if data exists

i'm new to cakephp and facing problems with AJAX requests
Here is the situtation, i'm registering a user with $.ajax jquery method:
function doregistration(){
if(isValidRegistrationForm()){
var frm = $(".frmregister").serialize();
$("#msgregister").html("Checking...");
$.ajax({
url: 'createuser',
type: 'POST',
data: frm,
success: function(result){
alert(result);
$("#msgregister").html(result);
if(result.indexOf("registered")>-1){
clearRegistrationForm();
}
}
});
}
}
i'm getting the value of username successfully in the following controller:
if($this->request->isAjax()){
$username = $this->request->data['username'];
$ar = $this->User->find("all", array('conditions' => array('User.username' => $username)));
The problem is $ar shows count($ar) as 1 but is not having any object
as if I try to write
$user = $ar[0];
it says no data at index 0.

Resources