For quite a while I'm failing to upload images through Ajax. I get a 500 internal error which I think relates to the fact that Ajax doesn't recognize that It's an image. I'm using this syntax which works well for comments, and likes. Images is where I have the problem. I'm not showing the controller since the request doesn't even get there.
$(document).ready(function(){
$(document).delegate(".send-image","click",function(e){
e.preventDefault();
var idval = this.id;
var file = $('input[type=file]').val().split('\\').pop();
console.log(file);
$.ajax({
url: 'store',
type: "post",
beforeSend: function (xhr) {
var token = $('meta[name="csrf_token"]').attr('content');
if (token) {
return xhr.setRequestHeader('X-CSRF-TOKEN', token);
}
},
data: {'id': idval, 'filename': file},
success:function(data){
console.log(data);
},error:function(){
console.log("error!!!!");
}
});
});
});
Related
I'm trying to create a search field on the top navbar with ajax. Since it's on the navbar, it has to be present on all pages, therefore the URL is constantly changing.
$.ajax({
type: 'GET',
url: CURRENT_URL,
dataType: 'json',
data: {
search: userSearch
},
success: function (){...
I'm working with Laravel, so i tried this on the navbar page:
<script>
var CURRENT_URL = "{{url()->current()}}"
</script>
The CURRENT_URL is displayed fine if i try to console log it, but ajax gives an error "Source map error: Error: request failed with status 404".
How can i insert the current URL into the ajax request?
You can use location.href instead of CURRENT_URL
$.ajax({
type: 'GET',
url: location.href,
dataType: 'json',
data: {
search: userSearch
},
success: function (){...
or
<script>
var CURRENT_URL = location.href;
</script>
Hopefully, this can help. Here's my approach to make my url dynamically I get the URL in every forms var URL = $('#example_form').prop('action'); then the URL variable must append or set via Js/Jquery. check my example below.
<script type="text/javascript">
var URL = $('#example_form').prop('action');
$.ajax({
type:'GET',
url: URL+'/clearContent',
beforeSend: function (xhr) {
var TOKEN = $('meta[name="csrf-token"]').attr('content');
if (TOKEN) {
return xhr.setRequestHeader('X-CSRF-TOKEN', TOKEN);
}
},
data:{
get_category_id : $('.parent-id').val(),
},
success:function(data){
if (data.response.status == true) {
// your codes here
}
},
dataType: 'json',
complete: function () {}
});
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]);
}
My AJAX request gives a 404 on POST but when I change it to GET it works fine. Why is this?
$('#navitem').click(function (e) {
e.preventDefault();
var level = $(this).attr('lvl');
$.ajax({
url: 'subjects.php',
data: { "subjid": level },
type: 'POST',
dataType: String,
complete: function () {
// window.location = 'subjects.php';
}
});
});
Both the pages are present in the same directory.
$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
$("#p1").text(data);
$("#tempForm").load('tv_form.php');
}
});
});
});
It is my code , in load() function when I call 'tv_form.html' file it works, but when i call 'tv_form.php' it doesn't work , error is 403 forbidden
where is the problem ? .html works, but .php doesn't work.
Add URL instead of file name.
$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
$("#p1").text(data);
$("#tempForm").load('URL to tv_form.php'); // www.example.com/file.php
}
});
});
});
I'm creating a CMS using jQuery and AJAX. When I click, my "Add Campaign" buttom, it creates a unique client ID in the DB and on a hard reload, the new client shows up in its container. I am trying to use ajax to reload the container on the fly and I'm not having the exact luck i am hoping for. I can get it to reload, but it's like it's pulling in descriptions of each of the clients as well!
function AddNewClient() {
dataToLoad = 'clientID=' + clientID + '&addClient=yes';
$.ajax({
type: 'post',
url: '/clients/controller.php',
datatype: 'html',
data: dataToLoad,
target: ('#clientssidebar'),
async: false,
success: function(html){
$('#clientssidebar').html(html);
},
error: function() {
alert('An error occured!');
}
});
};
Try:
function AddNewClient()
{
dataToLoad = 'clientID=' + clientID + '&addClient=yes';
$.ajax({
type: 'post',
url: '/clients/controller.php',
data: dataToLoad,
success: function(html){
$('#clientssidebar').html(html); },
error: function() {
alert('An error occured!'); }
});
}