Joomla 3 Custom Module not able to get Data from Ajax - joomla3.0

Hi i am trying to set field value of an input field using ajax by calling the function from my helper.php but it does not return anything.
Here is my mod_name.php File
$doc = JFactory::getDocument();
$js = <<<JS
(function ($) {
jQuery('#edit_sch').click(function() {
var value = 'cyannnnn',
request = {
'option' : 'com_ajax',
'module' : 'helloworld',
'method' : 'sch',
'data' : value,
'format' : 'raw'
};
jQuery.ajax({
type : 'POST',
data : request,
success: function (response) {
$('#edit_duration').val('dssd');
}
})
return false;
});
});(jQuery)
JS;
$doc->addScriptDeclaration($js);
Here is my helper.php file
<?php
class ModVedhikaSchedule {
public function schAjax()
{
return 'Hello Ajax World' ;
}
}

i think you're missing the url parameter in the ajax call function. Add the parameter and check again.

Related

display laravel array validation in javascript

Can anyone please tell me how to display the validation error in javascript? I am sending the request with ajax and want to display the errors.
blade code
$('input[id^="facility_name"]').map(function() {
return this.value;
}).get();
Laravel Validation
'facility_name.*' => 'required|string|min:3|max:255',
display error from js
$('.facility_name_error').html(data.responseJSON.errors.facility_name);
or
$('.facility_name_error').html(data.responseJSON.errors.facility_name[0]);
I attached the output of validation so how can I display this message to html
If your question is how can you access an object attribute with a dot (.) in the name, you can use the array way
data.responseJSON.errors['facility_name.0'][0]
you can access the error as the following:
in this example i will append the errors to li item you can do what you want
$.ajax({
type: 'POST',
url: 'your-url',
data: {your_data},
success: function (data) {
}
}).fail(function (jqXhr) {
var resp = jqXhr.responseJSON;
if (jqXhr.status === 422) {
var errorsHtml = '<ul>';
$.each(resp.errors, function (key, value) {
errorsHtml += '<li>' + value[0] + '</li>';
});
errorsHtml += '</ul>';
}
}).always(function () {
});

JSON POST using AngularJS

AngularJS is not creating JSON as desired. With code (below) , it generates array (serialized steam ) but not form . ie
I am getting
{
data : 'value',
date : 'value'
}
But wanted - JSON ie
{
"data" : "value",
"date" : "value"
}
The code for Angular to POST json is ( snippet )
<script>
// Defining angularjs application.
var postApp = angular.module('postApp', []);
// Controller function and passing $http service and $scope var.
postApp.controller('postController', function($scope, $http) {
// create a blank object to handle form data.
$scope.user = {};
// calling our submit function.
$scope.submitForm = function() {
// Posting data to php file
$http({
method : 'POST',
url : 'user.php',
data :JSON.stringify($scope.user),
headers : {'Content-Type': 'application/json'}
})
.success(function(data) {
if (data.errors) {
// Showing some error which has to come from server
} else {
$scope.message = data.message; //make the json
}
});
};
});
</script>
What should i do to get JSON and not Array ?
If you want your data in JSON format you should use JSON.parse();
$http({
method : 'POST',
url : 'user.php',
data :JSON.parse($scope.user),
headers : {'Content-Type': 'application/json'}
})
have you tried angular.toJson method?
Solved .
by using nothing ie.
json: $scope.user it works ...

How can i pass query params as arguments in angularjs http services

How can i pass query params as arguments in angularjs http services.
My controller code is:
FetchHeadCategoryService.get({
'officeJndi': officeJndi
}, function (response) {
$scope.headCategories = response;
});
Service.js
module.factory('FetchHeadCategoryService', [
'$resource',
'SYSTEM',
function($resource, SYSTEM) {
return $resource(SYSTEM.ENV.API_END_POINT
+ 'v1/utility/headCategories/:officeJndi ', {
officeJndi : '#officeJndi'
}, {
get : {
method : 'GET',
isArray : true
}
});
} ]);
HeadCategory.java
public Response fetchDocMgmtHeadCategory(#PathParam(value = "officeJndi") final String officeJndi, #QueryParam(value = "limitFrom") final int limitFrom,#QueryParam(value = "noOfRows") final int noOfRows){
..
..
..
}
I can obtain the result without passing the query params by managing them in the code.
But I want to send the value of query params "limitFrom" and "NoOfRows" to the service ,so that i acn fetch the data accordingly.Can somebody HELP.
Try to use params option to send additional data
get : {
method : 'GET',
isArray : true,
params: /* your data {} */
}

actions in $resource in angularjs

I have a webapi back end with the following method in the ProductController :
[HttpGet]
[Route("api/product/FindName")]
public Product FindName(string name){
return name & "Hello "
}
I am trying to make use of $resource in the frontend.
var resource = $resource('api/product/:id', {});
resource.query() will return all items which are exposed in the server side using the GetALL() method. This works fine .
What exactly is the {action} in the $resource does ? I have seen examples for the POST, but what if is set
var resource = $resource('api/product/:id', {}, { FindName: { method: 'GET', params: { name: 'phone' } } });
will this call the method FindName in the backend ? or what exactly it does, I mean the parameter if I set the 'GET' in method.
I am calling as
resource.FindName({ name: 'phone' }, function () {
});
But the backend is not getting fired . i see the call that is being requested to the server from fiddler is
Demo/api/product?name=phone
The resource declaration is incorrect. It should be
var resource = $resource('api/product/:id', {}, { FindName: { method: 'GET', params: { id: 'phone' } } });
This defaults the id placeholder to value phone.
For invocation now you can do
resource.FindName({}, function () { //gets api/product/phone
});
or override id part
resource.FindName({id:'tablet'}, function () { //gets api/product/tablet
});
Resource has a built in GET function that should be able to be used without the need to define the extra FindName action that has been added to $resource.
If you changed the route on your webapi to be
[HttpGet]
[Route("api/product/{name}")]
public Product FindName(string name){
return name & "Hello "
}
Then you could use resource like this to get data back from this route.
var resource = $resource('api/product/:id', {}, {});
resource.get({ id: 'phone' }, function () {
});
If you wanted the name params to match on both you could change :id to :name and in the resource.get change id to name also.
I hope this helps.

CakePHP: Unable to get returned data using AJAX

I want to get the value of a single variable from my Controller class using AJAX request. But it's still cannot get the returned variable. Here's the code I use:
$(document).ready(function(){
$("#mybutton").click(function(){
var userdata = {username : $("#UserUsername").val()};
$.ajax({
type:'POST',
url: 'http://localhost/mycakephp/tests/',
data: userdata,
success: function(data){
alert(data.str);
}
});
});
});
The controller class:
<?php
class TestsController extends AppController {
public $name = 'Tests';
function index(){
$this->autoRender=false;
$str = "valid";
return $str;
}
}
?>
I want to get '$str' variable, but why the alert() result is still "undefined", which is supposed to be "valid"?
That's because this is not how AJAX requests work. They can't capture any PHP return value as it's not a server-side request nor PHP at all. All it can do it catch the actual HTTP response it gets, being something like HTML or JSON. You can just simply echo the result in your function and it should do:
function index(){
$this->autoRender = false;
$str = "valid";
echo $str; // echo rather than return here
}
This way $str is then set in the data reposonse (so not data.str) of the AJAX request.
you need to updated jquery add index after function name
$(document).ready(function(){
$("#mybutton").click(function(){
var userdata = {username : $("#UserUsername").val()};
$.ajax({
type:'POST',
url: 'http://localhost/mycakephp/tests/index',
data: userdata,
success: function(data){
alert(data.str);
}
});
});
});

Resources