Ajax https is not applied in the laravel - ajax

laravel ver 9
url connecting now https://www.com
ajax code url:http://www.com
routes/web.php
route::get('/file/{doc_id}/research_list', [App\Http\Controllers\HomeController::class, 'research_list'])->name('research_filelist');
``
blade.php code
$(document).on("click", "#Recently_Uploadhistory_{{ $laboratory_documents->id }}", function() {
document.getElementById("data_wait_{{ $laboratory_documents->id }}").innerHTML =
loading_list;
$.ajax({
url: '{{ Route('research_filelist', $laboratory_documents->id) }}',
type: 'GET',
processData: false,
contentType: false,
success: function(data) {
$("#data_wait_{{ $laboratory_documents->id }}").empty();
document.getElementById(
"data_wait_{{ $laboratory_documents->id }}")
.innerHTML =
data;
console.log(data);
}
});
});
enter image description here
https please
cloudflare
enter image description here

Try using the route() helper like this in your ajax url:
{{ route('research_filelist', ['doc_id' => $laboratory_documents->id]) }}

Related

Laravel 8 Method Not Allowed 405 Ajax CRUD

I fixed my csrf
I fixed my route , route clear too
but still error show up .
I'm doing ajax form in modal :
My Route
Route::post('increp/store',[\App\Http\Controllers\IncrepController::class,'store'])->name('increp.store');
My ajax
// Create article Ajax request.
$('#submit_increp').click(function(e) {
e.preventDefault();
var data = $("#main-form").serialize();
$.ajax({
url: "{{ route('increp.store') }}",
type: 'POST',
data: data,
dataType: 'json',
beforeSend:function(){
$(document).find('span.error-text').text('');
},
success: function(result) {
if(result.errors) {
console.log(result.errors);
$('.alert-danger').html('');
$.each(result.errors, function(key, val) {
$('span.'+key+'_error').text(val[0]);
});
} else {
$('.alert-danger').hide();
$('.alert-success').show();
}
}
});
});
I doing this for days, i dont have idea how to fix this errors .
Network tab
Console tab

wordpress ajax form i found 404 when i submit it

i found 404 error
var endpoint='';
var bookingform=$("#BookingForm").serialize();
jQuery.ajax({
url: endpoint,
type: "POST",
data:{'action':'submitform1','bookingform':bookingform},
processData: false,
contentType: false,
success: function (result) {
$("#propertydata").html(dat.data)
},
error: function(dat1){
alert("error");
}
You are sending ajax to empty URL -> var endpoint='';
You need to send it to wordpress ajax url like this:
<script type="text/javascript">
var endpoint = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
You need to paste the above code in header.php file of your theme.
Secondly, in the success function, you are using the variable that is not defined (dat) you need to use something like this:
jQuery.ajax({
url: endpoint,
type: "POST",
data:{'action':'submitform1','bookingform':bookingform},
success: function (result) {
var dat = JSON.parse(result);
$("#propertydata").html(dat.data)
},
error: function(dat1){
alert("error");
}
});

Ajax in Django : url not found

I am coding a small Django project where an user can select an object and save it in a database. I am trying to implement an Ajax call on a button to delete this object if necessary.
I am doing it step by step, debugging with the console.
my urls:
app_name = 'register'
urlpatterns = [
path('<int:user_id>/', views.account, name='account'),
path('/delete/', views.delete, name='delete'),
]
my view.py:
def delete(request):
data = {'success': False}
if request.method=='POST':
product = request.POST.get('product')
print(product)
data['success'] = True
return JsonResponse(data)
my ajax.js:
$("#form_id").on('submit', function(event) {
event.preventDefault();
var product = 'coca-cola'
console.log('ok till this point')
$.ajax({
url: '{% url "register/delete" %}',
type: "POST",
data:{
'product':product,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
datatype:'json',
success: function(data) {
if (data['success'])
console.log('working fine')
}
});
});
My view isn't doing much for now but I haven't any knowledge about Ajax and I am doing it one step at a time.
This is the error I get in the console:
jquery.min.js:2 POST http://127.0.0.1:8000/register/6/%7B%%20url%20%22register/delete%22%20%%7D 404 (Not Found)
As far as I understand, Ajax can't find my url: '{% url "register/delete" %}'.
I have tried '{% url "register:delete" %}' with no luck either.
I found an answer after some tweaking, I defined my url before the Ajax call and then pass it in it:
$("#form_id").on('submit', function(event) {
event.preventDefault();
var product = 'coca-cola'
var url = '/register/delete/'
console.log( url)
$.ajax({
url: url,
type: "POST",
data:{
'product':product,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
datatype:'json',
success: function(data) {
if (data['success'])
console.log('working fine')
}
});
});
Also you can add just the string of url to "url" parameter without characters {% url %}. Maybe you copied the code from pattern Django and added it to JS-file. So it does not work.

ajax is not working in laravel

$(document).ready(function () {
$('#designation').change(function () {
var opt=this.value;
// var login_url = '{{ url("ajax_crud") }}';
alert(opt);
$.ajax({
//alert(opt);
type: "post",
url:'ajax_type' ,
data: {option:opt},
success: function(data){
alert('hiii');
//$("#div_dprtmnt").html(data);
//document.getElementById("department").innerHTML=data;
}
});
});
});
this is my view page.jquery is working.but inside the ajax alert is not working.how to use $.ajax in laravel.please help me
I think this is an issue with your CSRF token. Ensure you are attaching a token on each request to protect from forgery. You can do all this like this in ajax like this:
(document).ready(function () {
$('#designation').change(function () {
var opt = this.value;
$.ajax({
_token: "{{ csrf_field() }}",
type: "post",
url: "ajax_type",
data: {
option: opt
},
success: function(data){
alert('hiii');
}
});
});
});

Laravel - DELETE Ajax request

I am using Laravel 5.0. I have created a RESTful Controller. Now i wanna use the destroy function via an ajax call.
This is my JS:
$.ajax({
type: 'POST',
url: '/pv/' + data.id,
data: {_method: 'delete' },
success: function(data){
console.log(data);
}
});
And this is my destroy function:
public function destroy($id)
{
$pv = PV::find($id);
$pv->delete();
return true;
}
All i got is a 500 error.
first check your route in laravel
Route::delete('/deleteprocess', 'Controller#destroy');
in javascript
$.ajax({
url: '/deleteprocess',
type: 'POST',
data:{
'_token': $('meta[name=csrf-token]').attr("content"),
'_method': 'DELETE',
},
success: function(result) {
// Do something with the result
}});
set type : POST,
set token : _token,
set method : _method as DELETE,
That's probabily a "CSRF" Exception. If you are using Ajax Request with jQuery, add this (before your $.ajax):
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}
});
And, in your "html" ..create a new "meta tag" with csrf-token..value:
{{ csrf_token() }}
Read more: http://laravel.com/docs/5.0/routing#csrf-protection

Resources