Unable to receive ajax request in codeIgniter PHP - javascript

I have just started codeigniter and I am stuck at sending employee ID to my controller.
Actually, I have a datatable which shows all the registered employees, and there is a button which get the ID of the employee of row clicked and send it to controller through ajax call but i am unable to receive it in my ajax call.
JS code
$('#viewAllEmployeeTable tbody').on('click', '.viewEmployeeDetail', function() {
var data = viewAllEmployeeTable.row($(this).parents('tr')).data();
console.log(data);
employeeID = data.employeeID;
alert(employeeID);
$.ajax({
url: "/ackamarackus/employee/viewEmployeeProfile",
type: "GET",
data: {
"employeeID": employeeID
},
dataType: "json",
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
});
Controller
public function viewEmployeeProfile() {
$name = $this->input->post('employeeID');
echo "INPUT";
echo $name;
die();
}
This is what ajax is sending : employeeID:1000
Can anyone tell me what I am doing here? I have already tried google and stack overflow link, but nothing solved my problem. Thanks

You cannot send field data with type: 'GET'
Change this to type:'POST' and this will solve your issue :)

You are sending employeeID as GET method and accessing it as POST in Your controller, change your type in ajax request to POST as follows
$('#viewAllEmployeeTable tbody').on('click', '.viewEmployeeDetail', function() {
var data = viewAllEmployeeTable.row($(this).parents('tr')).data();
console.log(data);
employeeID = data.employeeID;
alert(employeeID);
$.ajax({
url: "/ackamarackus/employee/viewEmployeeProfile",
type: "POST", //Your problem here
data: {
"employeeID": employeeID
},
dataType: "json",
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
});

Related

Return json response in ajax request laravel

I have the following ajax code in my frontend.
var formData = $('#application-information-step-one').serialize();
var actionUrl = $('#application-information-step-one').attr('action');
$.ajax({
url: actionUrl,
type: 'POST',
data: formData,
success: function(data) {
var resp = JSON.stringify(data);
console.log(resp);
},
error: function(data) {
}
And following code to return json response from my controller if the request is ajax:
if ($request->ajax()) {
return response()->json([
'message' => 'Information saved sucessfully !'
],200)->headers('Content-Type', 'application/json');
}
But with the above code setup. Laravel returns HTML code of same page from where the request was made.
HTML page is rendered in preview section of network tab .
Can anyone suggest what am I missing here ?
Check for Ajax request is incorrect, otherwise the controller wouldn't have outputted HTML.
You can try several things:
use Illuminate\Support\Facades\Request; at the top of the file
replace request->ajax() with Request::wantsJson()
use request()->ajax()
Any of the above subject to your Laravel version.
More details here
Return JSON response from controller
if ($request->ajax()) {
return response()->json([
'status' => 'success',
'data' =>[],
'message' => 'Information saved successfully!'
], 200);
}
In AJAX success function code:
success: function (data) {
console.log(data.message);
},
and add to AJAX request object next parameters too
dataType: 'json',
contentType: false,
processData: false

jQuery AJAX POST method not working

I am trying to execute the AJAX operation. the call is working fine but the problem I am having is that I am getting an empty string as a response. There is no error in the console. all I get is an empty string. Even when I change the dataType to JSON, I still get the same response.
JavaScript Code:
$.ajax({
url: "data/saveCart.php",
method: "POST",
data: {
cartItem:item
},
dataType: "text",
success: function(data){
console.log(data);
}
});
PHP code:
if(isset($_POST['cartItem'])) {
echo "AJAX successful";
} else {
echo "AJAX failed";
}
It seems like it was caused by not stringifying your data.
var item = {
toothbrush: {
price: 1
}
};
$.ajax({
url: "data/saveCart.php",
method: "POST",
data: {
cartItem: JSON.stringify( item )
},
dataType: "text",
success: function(data){
console.log(data);
}
});

Am trying to send form data from ajax jquery to java controller but am getting 400 error

Am getting form data in console.log
$('#save').on('click',function(){
alert("test");
var data = $('form').serialize();
console.log("data"+data);
$.ajax({
type: "POST",
url: "saveExpenses",
data: $('form').serialize(),
cache: true,
success: function(data){
alert("Success");
}
})
})
and the controller:
#RequestMapping(value="/saveExpenses",method=RequestMethod.POST)
public String saveExpense(ExpensesSummary expenses, HttpServletRequest request,HttpSession session){
System.out.println("first name"+expenses.getFirstName());
String message = homeBankingDao.expenseSummary(expenses);
request.getSession().setAttribute("message",message);
return "login";
}
but am getting 400 bad request error.can anyone help me out in this..

jQuery: Ajax get request does not work with PHP

I created a simple script in order to pass a value in a PHP file.
This is the .js script:
$("#test").click(function () {
var id = 34;
$.ajax({
method: "POST",
url: "ajax.php",
data: {
id: id
},
success: function (data) {
alert("data sent");
},
error: function (data) {
alert("Data sending failed");
}
});
});
And this is the code included in the PHP file:
if (isset($_POST['id'])) {
$id = $_POST['id'];
echo $id;
}
The Ajax request works, but in the PHP file I receive an empty variable.
Check your $_GET global, if it's set there, you'll need to change method: "POST" to type: "POST"

Yii Ajax on click

I have some app on Yii. I want to implement ajax calls on click of div.
I've found some docs with form ajax validation, but it'snot clear for me, how can I do what I want. That's what I did:
$(document).on('click','div.lessonDiv', function()
{
$.ajax(
{
type: "POST",
url: "../../protected/controllers/AjaxController.php",
success: function(data, textStatus, jqXHR)
{
console.log(data);
}
});
}
That says that the directory is forbidden. Where should I put file which is able to interact with ajax? Or it alreay has it?
UPDATE I'm using version 1.1.
You shouldn't call files directly, Yii doesn't work this way. It is MVC framework with controllers and actions and it uses routes, also for AJAX request. So you should add an action to AjaxController and call createUrl to get it's URL.
PHP
class AjaxController extends CController
{
public funcion actionDoThing()
{
// Get request object
$request = Yii::app()->request;
// Check if request is acceptable
if ($request->isPost && $request->isAjaxRequest)
{
echo CJSON::encode(array('hello'=>'world'));
}
// else
// {
// throw new CHttpException(403);
// }
}
}
JS
$(document).on('click','div.lessonDiv', function() {
$.ajax({
type: "POST",
url: <?php echo $this->createUrl('ajax/doThing'); ?>,
success: function(data, textStatus, jqXHR) {
console.log(data);
}
});
});
I'd recommend to read Yii guide more closely. It is also available in Russian
Try this
$(function(){
$(document).on('click','div.lessonDiv', function()
{
$.ajax(
{
type: "POST",
url: "<?php echo Yii::app()->createUrl('Ajax/index'); ?>",
success: function(data, textStatus, jqXHR)
{
console.log(data);
}
});
});
});
To learn about createUrl() in YII click here

Categories