Ajax Request with Image base64 shows 403 Forbidden in Laravel - ajax

I am trying to crop and upload image with croppie plugin. The code is working fine on local server, but when I upload it on live server and upload an image, the POST URL shows 403 (Forbidden). I tried to search the solution online but could not find anything helpful. Can someone help me, the ajax request code is:
$('#cropImageBtn').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'viewport'
}).then(function (resimg) {
$.ajax({
url: "{{ route('profile.uploadimage') }}",
type: "POST",
data: {"image": resimg},
success: function (res) {
$('#item-img-output').attr('src', resimg);
$('#cropImagePop').modal('hide');
swal(
{
title: res.title,
text: res.text,
type: res.type,
allowOutsideClick: false
}).then(
function(){
location.reload();
}
);
}
});
});
});

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

CSRF token not working in a Laravel ajax call

I've checked the gizillion answers to this question and for some reason I can't get this to work.
I get the error:
401 (Unauthorized)
My route is an api route guarded.
My data:
let ajaxMainTemplate = {
'mainTemplate': mainTemplate,
'templateId': templateId,
'_token': accessToken,
}
My ajax call:
$.ajax({
type: 'POST',
url:`../api/aiMainTemplate/${ajaxData.templateId}`,
data: {
_token: ajaxData._token,
ajaxData
},
success: function(response) {
console.log(response)
}
})
I've tried the above to test based on another response. I put the token outside of the ajaxData object. I get the same error. I've also attempted:
$.ajax({
type: 'POST',
url:`../api/aiMainTemplate/${ajaxData.templateId}`,
headers: { 'X-CSRF-TOKEN': ajaxData._token },
data: ajaxData,
success: function(response) {
console.log(response)
}
})
Same.
I've also confirmed the token is there by adding a console.log Any ideas what I'm doing wrong with this?
My url was behind the api protected route so I had to do an ajax call to login into it via ajax.
The credentials are in a variable ajaxData.
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:8000/api/login',
data: ajaxData,
success: function(response) {
shopifyToken = response.success.token;
getListProducts(shopifyToken);
}
});
With the shopify token generated from this login it worked.

Trying to use react Link within ajax success function

I am doing a simple app with react and flask. At this moment i am trying to implement the routing, with react-router, of a successful registration. I planned on doing this inside the ajax success function but i am struggling a bit.
handleSubmit:function(evt){
var data = {
client: this.state.client,
name: this.state.name,
email: this.state.email,
password: this.state.password,
contact:this.state.contact
};
evt.preventDefault();
$.ajax({
type:"POST",
url: "/api/v1/register",
contentType: 'application/json',
dataType: "json",
data: JSON.stringify(data),
success:function(result){
if(result.result == "That account already exists"){
$("div.notification").html(result.result).show();
}
else if(result.result == "You didnt choose a user type"){
$("div.notification").html(result.result).show();
}
else{
console.log(result.result);
**I tried Link to and push.history.pushState and it didnt work**
}
},
error:function(){
console.log("error with ajax");
}
});
}

Google app engine JSON/AJAX not working

Here is my JS code:
<script>
$("#comments").click(function(event) {
$.ajax({
type: "GET",
url: '/localhost:8080/comment',
data: JSON.stringify(
{
'name': 'anon',
'subject': 'MY COMMENTS',
}),
contentType: 'application/json',
success: function(data,textStatus, jqXHR) {
console.log('POST response: ');
console.log(data);
}
});
});
</script>
and here is my Python Code:
class Guestbook(webapp2.RequestHandler):
def get(self):
Jguest_data = json.loads(self.request.body)
return self.response.out.write(json.dumps(Jguest_data))
I got the error 404 Resource not found. After digging around there is some issues with localhost . so I tried with JSONP as follows:
<script>
$("#comments").click(function(event) {
$.ajax({ // ajax call starts
url: "localhost:8080/comment", //
type: "GET",
data: JSON.stringify(
{
'name': 'anon',
'subject': 'MY COMMENTS',
}),
dataType: "jsonp", // Choosing a JSON datatype
success: function(data,textStatus,jqXHR)
{
console.log('POST response: ');
console.log(data);
}
});
});
</script>
That still does not work... I get "No JSON object could be decoded" Error.
I tried replacing JSON.loads with JSON.load... and that still errors out...
Can someone please let me know what the issue is?
Thanks a mil in advance
when you do a GET request with ajax you are using url parameters.
so there is no body in the request.
make a POST and change your get() to post()
You should edit your app.yaml file if there is one in your project, and if there is not one, add one.
Add this code to your file (app.yaml).
-url: /comment
script: <url-to-the-server-side-script>
You can see a more complete app.yaml documentation for use with python here.
https://cloud.google.com/appengine/docs/python/config/appref

How to send parameters with jquery $.get()

I'm trying to do a jquery GET and i want to send a parameter.
here's my function:
$(function() {
var availableProductNames;
$.get("manageproducts.do?option=1", function(data){
availableProductNames = data.split(",");;
alert(availableProductNames);
$("#nameInput").autocomplete({
source: availableProductNames
});
});
});
This doesn't seem to work; i get a null in my servlet when i use request.getParameter("option");
If i type the link into the browser http://www.myite.com/manageproducts.do?option=1 it works perfectly.
I also tried:
$.get(
"manageproducts.do?",
{option: "1"},
function(data){}
which doesn't work either.
Can you please help me?
EDIT:
also tried
$.ajax({
type: "GET",
url: "manageproducts.do",
data: "option=1",
success: function(msg){
availableProductNames = msg.split(",");
alert(availableProductNames);
$("#nameInput").autocomplete({
source: availableProductNames
});
}
});
Still getting the same result.
If you say that it works with accessing directly manageproducts.do?option=1 in the browser then it should work with:
$.get('manageproducts.do', { option: '1' }, function(data) {
...
});
as it would send the same GET request.
Try this:
$.ajax({
type: 'get',
url: 'manageproducts.do',
data: 'option=1',
success: function(data) {
availableProductNames = data.split(",");
alert(availableProductNames);
}
});
Also You have a few errors in your sample code, not sure if that was causing the error or it was just a typo upon entering the question.
I got this working : -
$.get('api.php', 'client=mikescafe', function(data) {
...
});
It sends via get the string ?client=mikescafe
then collect this variable in api.php, and use it in your mysql statement.
This is what worked for me:
$.get({
method: 'GET',
url: 'api.php',
headers: {
'Content-Type': 'application/json',
},
// query parameters go under "data" as an Object
data: {
client: 'mikescafe'
}
});
will make a REST/AJAX call - > GET http://localhost:3000/api.php?client=mikescafe
Good Luck.

Resources