I've got simple file upload form. Form works on desktop PC/Mac but doesn't work on iOS devices.
On submit event I make call like this:
var form_data = new FormData(this);
$.ajax({
url: "api/register",
method: "POST",
type: "POST",
headers: {
"cache-control": "no-cache"
},
cache: false,
data: form_data,
contentType: false,
processData: false,
async: true
}).success(function (res) {
alert ('uploaded');
}).error(function (res) {
alert('not uploaded')
});
After 60 seconds error is returned nad I get 'not uploaded' alert.
Related
Hey guys I'm trying to send files to Laravel server using ajax for that i need to create form data and send it with the ajax request and in order to prevent illegal invocation i need to set these
processData: false,
contentType: false,
heres my code->
$("#saveChanges").click(function () {
let formData = new FormData();
formData.append("banner", $("input[name='banner']"));
$.ajax({
url: "/save/website/data",
type: "POST",
data: {
pickUpBtnColor: $("input[name='pickUpBtnColor']").val(),
banner: $("input[name='banner']").val(),
logo: $("input[name='logo']").val(),
_token: $('meta[name="csrf"]').attr("content"),
formData: formData,
},
processData: false,
contentType: false,
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
},
});
});
But when i set these my csrf is also ignored and the server responds with "CSRF token mismatch."
Any one has a solution?
The issue is because you need to place all the form field values within a single FormData object and send that in the request.
$("#saveChanges").click(function() {
let formData = new FormData();
formData.append("banner", $("input[name='banner']"));
formData.append('pickUpBtnColor', $("input[name='pickUpBtnColor']").val());
formData.append('banner', $("input[name='banner']").val());
formData.append('logo', $("input[name='logo']").val());
formData.append('_token', $('meta[name="csrf"]').attr("content");
$.ajax({
url: "/save/website/data",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
},
});
});
Note that you could potentially simplify this if your input elements are contained within a form; hook to the submit event of the form element instead of the click of the button, and then call let formData = new FormData(referenceToYourFormHere);. There is no need for the append() calls when using the constructor in this manner.
How do I make autoload/reload in every 5 seconds in ajax?
This is the code I use. The problem is new data doesn't appear, unless I reload it.
$(document).ready(function(){
// temp
$.ajax({
url : 'assets/php/act_now/module_loadChat.php',
type: 'POST',
async: false,
success: function(show_chat) {
$('#direct-chat').html(show_chat);
},
cache: false,
contentType: false,
processData: false
});
// temp
$("#text-on-it").submit(function(event){ // when send message
event.preventDefault();
var formData = new FormData(this);
$.ajax({
url: 'assets/php/act_now/module_loadChat.php',
type: 'POST',
data: formData,
async: false,
success: function(data) {
$('#direct-chat').html(data);
$("#text-on-it")[0].reset();
},
cache: false,
contentType: false,
processData: false
});
});
});
setInterval(function(){
// this will run after every 5 seconds
}, 5000);
I got struck in a point where i have to pass dataForm data's and other datas through a single ajax call, Actually am passing a Blob data by creating a DataFrom object Following code will give you a exact explanation
my ajax call with data's
Fr.voice.export(function(blob){
var data = new FormData();
data.append('file', blob);
console.log(blob);
$.ajax({
url: "upload1.php",
type: 'POST',
data: data,
contentType: false,
processData: false,
success: function(data) {
// Sent to Server
}
});
}, "blob");
In the above Ajax call am POSTing only the blob data where as i have to pass other data's like
id: student_id,
test_no: test_no,
attempt_no: attempt_no,
question_name: "audio" + audioNo.
What i have tried
Fr.voice.export(function(blob){
var data = new FormData();
data.append('file', blob);
console.log(blob);
var postData = {
"audio": base64,
"id": student_id,
"test_no": test_no,
"attempt_no": attempt_no,
"question_name": "audio" + audioNo
};
$.ajax({
url: "upload1.php",
type: 'POST',
data: {data,postData},
contentType: false,
processData: false,
success: function(data) {
// Sent to Server
}
});
}, "blob");
Am getting [object,object] while am posting the data.
Am new to php and Ajax call, please help me to solve this. Thank you in advance.
Have you tried it like this:
$.ajax({
url: "upload1.php",
type: 'POST',
data: { data: dataVar, postData: postDataVar},
contentType: false,
processData: false,
success: function(data) {
// Sent to Server
}
});
I am having a div
that shows a loader image while sending ajax requests
I attached the div in the beforeSend attribute of ajax. The issue is if I send serialize() data then the beforeSend works but doesn't work when I send formData
Works
$.ajax({
url: 'centre-basic-process.php',
type: 'post',
data: $('#centreform').serialize(),
beforeSend: function() {
$('.displayLoader').show();
},
success: function(response) {
console.log(response);
$('#centreformupload').show();
$('#centreform').hide();
$('html, body').animate({
scrollTop: 0
}, 0);
}
});
Doesn't work
$.ajax({
url: 'centre-upload-process.php',
type: 'POST',
data: formData,
async: false,
beforeSend: function() {
$('.displayLoader').show();
},
success: function(response) {
alert("You have succesfully submitted the form. Please complete the payment method");
document.location.href = "dashboard.php";
},
cache: false,
contentType: false,
processData: false
});
I even tried ajaxStart() but facing same issue. Can anyone help me?
I am using ajax function for checking username and password but some android device does not support it. so what i have to do? This is my ajax function
$(function()
{
$("#loginform").submit(function()
{
document.getElementById('loading').style.display = "block";
var formData = new FormData($("#loginform")[0]);
$.ajax({
url: 'login_check.php',
type:'POST',
data: formData,
processData: false,
contentType: false,
cache: false,
mimeType: 'multipart/form-data',
success: function(html)
{
}
});
return false
});
});
Probably your issue relates not to ajax not being available, but to a bad browser response. See this.