Created
March 14, 2018 18:14
-
-
Save jonathansanchez/5fa7c89a7b2d325af48753a22e2a9a41 to your computer and use it in GitHub Desktop.
Send AJAX POST file on Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
class ActivationPostPagoController extends Controller | |
{ | |
. | |
. | |
. | |
/** | |
* * Callback AJAX | |
* | |
* @param Request $request | |
* @return mixed | |
*/ | |
public function myData(Request $request) | |
{ | |
$inputs = $request->all(); | |
. | |
. | |
. | |
} | |
. | |
. | |
. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layout') | |
@section('content') | |
<article> | |
<form id="form" method="POST" enctype="multipart/form-data"> | |
<div class="form-group has-feedback"> | |
<label for="input-file">Foto Cédula frontal*</label> | |
<input type="file" id="input-file" name="myfile" /> | |
</div> | |
<input type="hidden" name="_token" value="{{ csrf_token() }}"> | |
<button type="submit" id="submit">Send</button> | |
</form> | |
</article> | |
@stop | |
@section('script') | |
<script type="application/javascript"> | |
$(document).ready(function () { | |
$("#submit").click(function(e) { | |
e.preventDefault(); | |
$(this).text('Sending...'); | |
$(this).prop('disabled', true); | |
sendForm(); | |
}); | |
function sendForm() { | |
var formData = new FormData($("#form")[0]); | |
$.ajax({ | |
headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'}, | |
url: '/some/url', | |
type: 'POST', | |
data: formData, | |
dataType:'JSON', | |
success: function(data) { | |
console.log(data); | |
if (data.code === 'OK') { | |
console.log("OK"); | |
return; | |
} | |
console.log("Not OK"); | |
}, | |
error: function() { | |
console.log("Error"); | |
}, | |
processData: false, //This is very important!! | |
contentType: false //This is very important!! | |
}); | |
} | |
}); | |
</script> | |
@stop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment