In my Laravel Projects, I want to pass data from blade to my controller using Ajax.
But I am getting error
404 | Not Found
ajax
var pi_id = $(this).attr('href');
$.ajax({
url: "{{ url('purchase-invoice-detail') }}",
type: "get",
data: {
'id': pi_id
},
success: function(response){ // What to do if we succeed
alert('ok');
},
error: function(response){
alert('Error'+response);
}
});
route
Route::get('purchase-invoice-detail/{id}', 'PurchaseController#purchase_invoice_detail')->middleware('auth');
controller
public function purchase_invoice_detail($id)
{
return 1;
}
Can't find the problem.
Anybody Help Please ?
Please try this code
"{{ url('purchase-invoice-detail') }}" + '/' + pi_id
Related
my route is
Route::get('functionvalueupdate',[\App\Http\Controllers\AdminController::class,'functionvalueupdate'])->name('functionvalueupdate');
in script
$.ajax({
type: "GET",
url: "{{route('functionvalueupdate')}}",
data: {
id: id,
},
success: function(res) {
}
}
)
controller
public function functionvalueupdate(Request $request)
{
dd($request);
}
Console log error
allfunctionalarea:1 POST http://localhost/FESROZGAR/allfunctionalarea 405 (Method Not Allowed)
still i get this error The POST method is not supported for this route. Supported methods: GET, HEAD.
Something is not right here.
Your route, show that GET to functionvalueupdate .
However, your console log show at different route. Which is
http://localhost/FESROZGAR/allfunctionalarea . I didn't see any route to functionvalueupdate.
Or something like http://localhost/FESROZGAR/functionvalueupdate .
Please double check your POST or GET to the correct route. You can inspect element to see if the {{ route('functionvalueupdate') }} print the right route.
Change get to post in route as given below:
Route::post('functionvalueupdate'[\App\Http\Controllers\AdminController::class,
'functionvalupdate'])->name('functionvalueupdate');
Also change ajax type from get to post and don't forget to include csrf token in ajax request.
$.ajax({
type: "POST",
url: "{{route('functionvalueupdate')}}",
data: {
id: id,
"_token": "{{ csrf_token() }}",
},
success: function(res) {
console.log(error);
},
error:function(error) {
console.log(error);
}
}
)
Just try this for make the debuging better
$.ajax({
type: "GET",
url: "{{route('functionvalueupdate')}}",
data: {
id: id,
},
success: function(res) {
console.log(res);
},
error:function(error) {
console.log(error);
}
}
)
And please add the console.log error in your question and add your controller to the your question too
I try to update data using ajax and laravel it working in local but in production, it's given me an error (The requested URL /appointment/45/edit was not found on this server)
I am using ajax, laravel 5.7
$(document).on('click', '.edit', function() {
id = $(this).attr('id');
$.ajax({
url: "/appointment/" + id + "/edit",
dataType: "json",
success: function(html) {
$('#name').val(html.data.name);
$('#appdate').val(html.data.appdate);
$('.modal-title').text("Edit Appointment");
$('#action_button').val("Edit");
$('#action').val("Edit");
$('#modal-default').modal('show');
}
})
});
route
Route::resource('appointment','AppointmentController');
controller
public function edit($id)
{
if(request()->ajax())
{
$data = Appointment::findOrFail($id);
return response()->json(['data' => $data]);
}
}
The requested URL /appointment/45/edit was not found on this server
I try to update data using ajax and laravel it working in local but in production, it's giving me an error (The requested URL /appointment/45/edit was not found on this server)
In ajax call use named route like
url:'{{route('route.name')}}'
And also add ajax call type
type:'post' or type:'get'
In js:
$(document).on('click', '.edit', function() {
var baseurl = window.location.protocol + "//" + window.location.host;
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
id = $(this).attr('id');
$.ajax({
url: baseurl + "/appointment/" + id + "/edit",
type: 'get',
dataType: "json",
cache: false,
success: function(response) {
$('#name').val(response.data.name);
$('#appdate').val(response.data.appdate);
$('.modal-title').text("Edit Appointment");
$('#action_button').val("Edit");
$('#action').val("Edit");
$('#modal-default').modal('show');
}
})
});
In route file (web.php)
Route::resource('appointment','AppointmentController');
In controller:
public function edit($id)
{
$data = Appointment::findOrFail($id);
return response()->json(['data' => $data]);
}
I am getting error in my AJAX response.
I am trying to view a specific patient details while clicking 'view' button from 'All Patient' table using AJAX request.
But getting error in response like below image
But I returned an 'id' from my controller.
AJAX
$(".view_prescription").click(function(){
var id = $(this).val();
var token = $('input[name=_token]').val();
$.ajax({
type:'GET',
url:'/view-prescription',
data:{
_token : "{{ csrf_token() }}",
id: id
},
success:function(data) {
alert(data);
}
});
});
Route
Route::get('/view-prescription','PrescriptionController#view_prescription');
Controller
public function view_prescription(Request $request)
{
$id = $request->id;
return $id;
}
I understand that it's a slight mistake. But I can't figure that. Anybody help please
I have solved the issue.
just change url from '/view-prescription' to "{{url('/view-prescription')}}" and comment our 'token'.
$(".view_prescription").click(function(){
var id = $(this).val();
//var token = $('input[name=_token]').val();
$.ajax({
type:'GET',
url:"{{url('/view-prescription')}}",
data:{
_token : "{{ csrf_token() }}",
id: id
},
success:function(data) {
alert(data);
}
});
});
Can someone please show a Laravel 5.7 post ajax example with a full-working minimum example in a blade template? I know there are some resources in the web, but I miss a concise, straight-forward minimum example.
You can do something like this,
web.php
Route::post('/admin/order/{id}', 'OrderController#edit')->name('admin.order.edit');
blade.php
$(document).on('click', '.delete-button', function (e) {
e.preventDefault();
var orderId = 1
$.ajax({
type: 'post',
url: '/admin/order/' + orderId,
data: {
'_token': $('input[name=_token]').val(),
'data_one': 'dataone',
},
success: function () {
toastr.success('Order Has Been Deleted Successfully.');
},
error: function(XMLHttpRequest) {
toastr.error('Something Went Wrong !');
}
});
});
$.ajax({
url: 'http://some.working/url',
type: "POST",
data: $('#formContainer').serialize(),
success: function (response) {
console.log('Success', response);
},
error: function (response) {
console.log('Error', response);
}
});
The data can be produced in many ways for example
1. Using serialize() method as shown in the above example.
2. Using FormData():
for example
var data = new FormData($('#formContainer'));
In both of the above example, one thing compulsory is that your form
must contain csrf field. which can be provided using any of the
following methods:
<input type="hidden" name="_token" value="{{ csrf_token() }}" >
or
{{ csrf_field() }}
or even more simply by just using
#csrf
in some where in your form.
In case you are not using any form, you can create the data object by
yourself like this
var data = {
_token: '{{ csrf_token() }}',
data1: 'Value1',
data2: 'Value2',
data3: 'Value2'
}
Define a Web Route
Route::get('currencies/fiat/changeStatus','FiatCurrencyController#changeStatus')->name("currencies.fiat.chanageStatus");
Call this function on click onclick="changeStatus(1,0)"
function changeStatus(id,status){
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$.ajax({
/* the route pointing to the post function */
url: '/currencies/fiat/changeStatus',
type: 'GET',
/* send the csrf-token and the input to the controller */
data: {_token: CSRF_TOKEN,cid:id,status:status},
dataType: 'JSON',
/* remind that 'data' is the response of the AjaxController */
success: function (data) {
console.log(data);
}
});
}
That's it all Done.
$(document).ready(function(){
/* In laravel you have to pass this CSRF in meta for ajax request */
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
/* change in website layout base on click */
$('#user_view').on('click',function (e){
e.preventDefault();
$('.loading_screen').show();
var val = $(this).data('id');
$.ajax({
url: base_path + 'user/changeview/'+val,
type: "POST",
success: function (data) {
var obj = jQuery.parseJSON(data);
if (obj.SUCC_FLAG == 0){
window.location.href = site_url;}
else{
/* for console error message. */
console.log(obj.MSG);}
$('.loading_screen').hide();
},
error: function () {
alert("server error");
}
});
});
});
Hey it's a working code and i hope this will works for you.
I am new with laravel, I am having an issue when posting my data to controller in laravel while using ajax. My view contains a select box which is populated from database when a user select a value from select box i am trying to call ajax which will return details of salary of the user.The issue is it gives me 404 not found error.
The Code for the controller file is shown below.
this function is defined inside a controller
public function getPostSalary()
{
echo "ajax called";
return 'true';
}
The Routes file is
Route::post('company/salary-user', 'CompanyController#getPostSalary');
this is my ajax code from where the controller is being calleD
<script>
$(document).ready(function() {
$('#employee').change(function () {
var values = $('#employee').val();
if (values == '') {
$.pnotify({
title: 'Message',
text: 'Please Select a User.',
type: 'error',
delay: 3000
});
return false;
}
var csrf = $('#csrf').val();
$.ajax({
url: '{!! URL::to("company/salary-user")!!}',
type: "POST",
data: { user_id: values, _token: csrf },
cache: false,
beforeSend: function () {
},
success: function (data) {
console.log(data);
$('#loader').hide();
}
});
});
});
</script>
Can someone help me to figure out what is the issue and what should be done to call my function.
Single quotes is for string so your url is not generating as you expecting. Use like this
url : "{!! URL::to('company/salary-user')!!}",
You can use direct url like
$.ajax({
url:'company/salary-user',
type: "POST",
data: { user_id: values, _token: csrf },
cache: false,
beforeSend: function () {
},
success: function (data) {
console.log(data);
$('#loader').hide();
}
});
replace this line:
{!! URL::to("company/salary-user")!!}
by :
{{ url('/company/salary-user') }}
or
{{ route('/company/salary-user') }}