I use laravel and I want to upload gallery and I want to use ajax.
I created this code
$('#hello').on('submit', function(e){
e.preventDefault();
var formData = new FormData(this);
$.ajax({
method: "POST",
url: "{{ route('upload.store')}}",
mimeType: "multipart/form-data",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: formData,
success: function(done){
console.log(done)
},
error: function(error){
console.log(error);
}
});
});
This is ma the latest code. Could you help me?
$('#hello').on('submit', function(e){
e.preventDefault();
var formData = new FormData($("#hello")[0]);
$.ajax({
url: "{{ route('upload.store')}}",
type: "POST",
data: formData,
contentType: false,
cache: false,
processData:false,
dataType: 'json',
success: function(done){
console.log(done)
},
error: function(error){
console.log(error);
}
});
});
Related
<script>
$(document).ready(function(){
$(document).on('change', '#avatar', function(){
var property = document.getElementById("avatar").files[0];
var form_data = new FormData();
form_data.append("avatar", property);
$.ajaxSetup({
beforeSend: function(xhr, type) {
if (!type.crossDomain) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
}
},
});
$.ajax({
url: '{{ url('app()->getLocale())/profile') }}',
method: "POST",
data: form_data,
contentType: false,
cache: false,
processData: false,
success:function(response){
window.location.href = window.location.href;
$.notify({
message: 'Hello World'
},{
// settings
type: 'danger'
});
}
})
});
});
</script>
How can I do this so that when the image is finished uploading and the image is refreshed to display a message using $ .notify? I tried everything I found on the internet, but to no available...
I'm trying to post an image and some text via ajax onto my laravel server except I can't seem to add the File into the ajax request.
I have tried making a FormData and appending the needed params, I also tried serializing my form with jQuery.
$("#create-post-button").click(function(){
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
// var formData = new FormData();
// formData.append('src', $('#src')[0].files[0]);
// formData.append('title', $);
// formData.append('_token', CSRF_TOKEN);
// formData.append('_method', 'POST');
event.preventDefault();
console.log($('#src')[0].files[0]);
$.ajax({
headers: {
'X-CSRF-TOKEN': CSRF_TOKEN
},
url: '/posts/create',
type: 'POST',
data:
{
'_method': 'POST',
'_token': CSRF_TOKEN,
'title':$("#title").val(),
'src': {
'name':$('#src')[0].files[0].name,
'size':$('#src')[0].files[0].size
}
},
dataType: 'json'
});
});
I expect that when I dump my $request in laravel, that it has the correct request params but also including the $file (FileBag) param for the file that is being posted.
EDIT:
I have looked up the link #charlietfl provided in the comments and it helped me a lot, so here is the end result:
$("#create-post-button").click(function(){
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
event.preventDefault();
var file_data = $('#src').prop('files')[0];
var form_data = new FormData();
form_data.append('_method', 'POST');
form_data.append('_token', CSRF_TOKEN);
form_data.append('title', $('#title').val());
form_data.append('src', file_data);
$.ajax({
url: '/posts/create',
dataType: 'text',
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(){
showSuccessUploadingPost();
},
error: function() {
showErrorUploadingPost();
}
});
});
The CSRF token at file upload required to pass as a GET parameter.
$("#create-post-button").click(function(){
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
var form_data = new FormData();
form_data.append('title', $('#title').val());
jQuery.each(jQuery('#src')[0].files, function(i, file) {
data.append('src', file); //use the following line to handle multiple files upload
// data.append('src' + i, file);
});
$.ajax({
url: '/posts/create?_token=' + CSRF_TOKEN,
data: form_data,
cache: false,
contentType: false,
processData: false,
method: 'POST',
type: 'POST', // For jQuery < 1.9
success: function(){
showSuccessUploadingPost();
},
error: function() {
showErrorUploadingPost();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<form id="uploadFrm" enctype="multipart/form-data" method="post">
<input type="file" name="src" />
<button id="create-post-button">Upload</button>
</form>
function regCall(token){
$.ajax({
type: 'GET',
url: 'http://localhost:3000',
dataType: 'HTML',
headers: {
'x-auth': token
}
});
}
This is my ajax GET request I want to display the given url in html.
The below is the whole snippet with my login logic.
$(document).ready(()=>{
$('#login').submit((e)=>{
$.ajax({
type: 'POST',
url:'http://localhost:3000/login/users',
data: {
email: $('#email').val(),
password: $('#password').val()
},
success: function(data, status, req){
// alert(req.getResponseHeader('x-auth'));
localStorage.setItem('t',req.getResponseHeader('x-auth'));
var token = localStorage.getItem('t');
regCall(token);
// window.location.href = '/';
},
error: function (req, status, error) {
// alert(req.getResponseHeader('x-auth'));
localStorage.setItem('t',req.getResponseHeader('x-auth'));
alert('Invalid email and password');
window.location.href = '/login';
}
});
e.preventDefault();
});
})
This is the whole code of snippet.
extract response data from SUCCESS function:
function regCall(token){
$.ajax({
type: 'GET',
url: 'http://localhost:3000',
dataType: 'HTML',
headers: {
'x-auth': token
},
success: function(data){
//targetElement should be replaced by the ID of target element
$("#targetElement").html(data);
}
});
}
By using the success callback function you can display the response content on the HTML place
**First method:**
function regCall(token){
$.ajax({
type: 'GET',
url: 'http://localhost:3000',
dataType: 'HTML',
headers: {
'x-auth': token
},
success: function(responseData){
$("#div or class Id").html(responseData);
}
});
}
**Second method:**
function regCall(token){
$.ajax({
type: 'GET',
url: 'http://localhost:3000',
dataType: 'HTML',
headers: {
'x-auth': token
}
}).done(function(responseData){
$("#div or class Id").html(responseData);
});
}
**NOTE:**
Make sure you are having the jQuery script
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
here is my code
$.ajax({
url: "DataGridServlet.htm",
type: "GET",
traditional: true,
dataType: 'JSON',
cache: false,
contentType:"application/json",
success: function (response){
console.log(response);
}
});
});
and when i am sending request to controller.. every is working but wnem returning JSONObject i am getting thya status code as 406 not acceptable.
and below spring controller code
#RequestMapping(value="/DataGridServlet.htm", method = RequestMethod.GET,produces="application/json",headers="Accept=*/*")
public #ResponseBody JSONObject getReturnData()
{
System.out.println("control came into conroller");
JSONObject dataObject=new JSONObject();
dataObject=jqTabsGridDataDao.getTabsData();
System.out.println("controller data"+dataObject);
return dataObject;
}
any one can help me?
Change
#RequestMapping(value="/DataGridServlet.htm", method = RequestMethod.GET,produces="application/json",headers="Accept=*/*")
to
#RequestMapping(value="/DataGridServlet.htm", method = RequestMethod.GET,produces="application/json",)
and this
$.ajax({
url: "DataGridServlet.htm",
type: "GET",
traditional: true,
dataType: 'JSON',
cache: false,
contentType:"application/json",
success: function (response){
console.log(response);
}
});
});
to
$.ajax({
url: 'DataGridServlet.htm',
type: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
success: function(result) {
alert(result + " result ");
},
error: function(resError) {
//alert(resError + " Error ");
}
});
http://jsfiddle.net/YcK5X/
I'm wondering why this AJAX request doesn't return anything.
$.ajax({
type: 'POST',
url: '/echo/html',
data: 'Echo!',
success: function(data) {
$('#ajax').html(data);
},
dataType: 'text/html'
});
The data you want echo:ed must be supplied in a POST parameter named html:
$.ajax({
type: 'POST',
url: '/echo/html/',
data: {
'html': 'Echo!'
},
success: function(data) {
$('#ajax').html(data);
},
dataType: 'text/html'
});