Cakephp ajax request invalid url - cakephp

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')); ?>

Related

Not receiving response - ajax, codeigniter

Ajax | angular controller,
$scope.counting=function(id){
if(id){
$.ajax({
type: "POST",
url: baseUrl()+'/index.php/site/count',
data:{"id":id},
dataType: "json",
contentType: 'application/json',
success: function(data) {
console.log(data);
},
error: function() {
console.log('no response');
}
});
}
Output
no response
Codeigniter controller,
public function count()
{
$input_data = json_decode(trim(file_get_contents('php://input')), true);
print_r($input_data); die;
}
I am getting the post data, but I am unable to receive any response.
Problem seems to be at the json_decode part, but it looks fine.
View,
<li ng-click="counting(<?php echo $value->id;?>);" class="count"><button type="submit" name="count"> <i class="fa fa-heart-o"></i></button></li>
From your controller send response back to js in json format using json_sncode().Because print_r() prints array in php, js will not recognize it. but you can see array in network tab of console as response.
public function count()
{
$id= $this->input->post('id');
$input_data = array('id'=>$id);
echo json_encode($input_data);
}
I have found a more legit solution to the problem.
I removed the button tag in the view,
<li ng-click="counting(<?php echo $value->id;?>);" class="count"> <i class="fa fa-heart-o"></i></li>
Doing so, I do get the response.
I figured, the 'type=submit' was posting the button value directly to the codeigniter controller, and also the 'li' was calling the ajax function, that was also posting the 'id' to the controller.
But, since the 'button type submit' was redirecting to the function count(or to say a new page), the response was unable to show as the response was coming to the previous controller function( or rather i should say previous page). Even removing the 'type=submit' would work in such case.

how to change url dynamically when using $ngResource in angularJs

Here is my code
Const User = $resource('/user/:id', {},
{
'query': {
method: 'GET',
url:'/user/phone/:phone/name/:name',
isArray: true
}
});
When I type:
User.query({name: Li});
User.query({phone: 123456});
Http request will be:
get /user/phone/name/Li
get /user/phone/123456/name
But I hope it to be:
get /user/name/Li
get /user/phone/123456
Could someone tell me how to remove part substring in url dynamically? Must I use $http to generate different url respectively?
My English is poor. Thanks!

Why does my $http put not actually send any data?

I am using the following code block:
app.putConfigs = function () {
$http({
data: app.config,
headers: app.user.getHeaders(),
url: '/api/Config/Put',
method: "PUT"
});
}
As far as I can see this is the same as in the angular documentation. app.config is an object.
When I check with fiddler no JSON data is being sent to the server.
Is there something wrong with the way I am using $http ?
I would start with something simple to help diagnose your problem. See if this works:
$http.put('/api/config', { key: 'test' }).then(function(resp) {
console.log(resp);
});
Docs ref: https://docs.angularjs.org/api/ng/service/$http#put

ng-resource url parameter does not work

$resource is not correctly passing along a url parameter when making a PUT request, via custom action.
This is my service creating the resource.
.factory('cartItemsService', ['$resource', function($resource) {
return $resource('/api/cart/:cartId/items/', {format: 'json'}, {
get: {method: 'GET', isArray: true},
update: {method: 'PUT', isArray: true},
});
}])
In my controller I'm trying to update the list of items like this. Note that $scope.cart.id exists and is correct (in this case 1)
$scope.cartItems = cartItemsService.update({cartId: $scope.cart.id});
However the request URL is: /api/cart/items/ but I'm expecting /api/cart/1/items/. This works fine if I do .get({cartId: <some_id>}) but doesn't seem to work for update.
EDIT: Angular version 1.1.5
In the end this was due to the request headers I was setting before making the request.
I was attempting to set put headers like such:
$http.defaults.headers.put['X-CSRFToken'] = $cookies.csrftoken;
This is what caused the request url to be incorrectly formatted.
Changed it to set the post header instead, and it worked.
$http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;

Post values to cakePHP controller action and display it

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

Resources