I have a form and a save button. On click, I want to post all that data from the form to my Controller using Ajax. I could have done that using Input::all(), but I'm trying to avoid my page to refresh.
I want to be able to access those form data, just like input::all(), but using Ajax instead.
How would one go about and implement something like this ?
I've tried
JS
$('.saveRateLimitBTN').click(function (event) {
var url = '{{env("APP_URL")}}{{$cpe_mac}}'+'/device/'+'{{$device_mac}}'+'/rate/update';
var inputs = {};
$("#editRateLimitForm :input").each(function() {
inputs[$(this).attr("name")] = $(this).val();
});
$.ajax({
url: url,
type: 'PUT',
dataType: 'json',
data: inputs,
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('PUT error.', xhr, textStatus, errorThrown);
}
});
});
DeviceController > updateRate() function
public function updateRate(){
dd('Here'); // This never get run.
}
Route
Route::put('{cpe_mac}/device/{device_mac}/rate/update',['as'=>'device.update', 'uses'=>'DeviceController@updateRate']);
Form (Blade Syntax)
{!! Form::open(array('url' => '/'.$cpe_mac.'/device/'.$device_mac.'/rate/update', 'class' => 'form-horizontal', 'role' =>'form','method' => 'PUT', 'id' => 'editRateLimitForm')) !!}
<span class="pull-left">
<div class="col-sm-6">
<p>Downlink </p>
<select type="text" id="rateDownSelect" class="form-control" name="max_down" >
@foreach ($rate_limit as $key => $value)
<option value="{{$value or ''}}"
>{{$value or ''}} Kbps</option>
@endforeach
</select>
</div>
<div class="col-sm-6">
<p>Uplink</p>
<select type="text" id="rateUpSelect" class="form-control" name="max_up" >
@foreach ($rate_limit as $key => $value)
<option value="{{$value or ''}}">{{$value or ''}} Kbps</option>
@endforeach
</select>
</div>
</span><br>
{!! Form::hidden('cpe_mac', $cpe_mac)!!}
{!! Form::hidden('device_mac', $device_mac)!!}
<span class="pull-right">
<button class="saveRateLimitBTN btn btn-xs btn-info pull-right" type="button">Save</button>
</span>
{!! Form::close();!!}
Result
XMLHttpRequest cannot load http://localhost/000D6751560C/device/0800277B6BDE/rate/update. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.