i'm making an ajax call from my laravel blade view like this:
function deleteUser(user_id)
{
$.ajax({
url: '{!! route('users.delete', ['id' => user_id]) !!}',
type: 'DELETE',
data: {_token: '{!! csrf_token() !!}'},
dataType: 'JSON',
success: function (data) {
console.log(data);
}
});
}
and in my routes.php file i defined the route:
//routes.php
Route::delete('users/{id}/delete', 'UserController#delete')->name('users.delete');
where i'm just refreshing the page an error was occured:
Use of undefined constant user_id- assumed 'user_id'
i realized that the user_id parameter is injected by the wrong way inside the route() method within javascript block.
is there a way to inject parameters in the url of ajax call inside the route method of laravel?
You can't use Laravel helpers in your JS, unless your JS code building dynamically (which is bad idea). So just use manually built URLs.
url: 'users/145/delete',
You can trying doing it this way '{{url("users/$user->id/delete")}}'; your route will pick it up and redirect where it needs to go
Related
I have been working on this for hours,and not getting anywhere. Essentially, my Ajax call appears to be working well. I am pulling in form data from a view. I can see the XHR data when I look into Chrome Network. I have a status 200, and "OK". However, no matter what I do, I can not get any data to show in my controller. I have tested my route by commenting it out and sure enough I get an error, so that is positive. Also the XHR data shows the correct url as well. But even just a simple dd($request) inside the controller gives me nothing, not even an empty []. I have tried every option and Googled this to death. What am I doing wrong - Thanks !
Abbreviated HTML
<form action="" method="POST" id="formData">
<fieldset>
{{ Form::hidden('customer_id',$customer[0]->id,['id'=>'customer_id'])}}
{{ Form::hidden('lastname1',$customer[0]->lastname1,['lastname1'=>'lastname1'])}}
{{ Form::hidden('reference',$quotation[0]->reference,['id'=>'reference'])}}
</fieldset>
</form>
AJAX
$("#editsubmit").on('click', function () {
event.preventDefault();
var formData =$("#formData").serialize();
var id = $("#reference").val();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: 'POST',
contentType:'JSON',
processData: false,
url: '/save_edit/'+id,
data: {
"_method": 'POST',
"result": formData,
},
error: function () {
alert('there has been a system level error - please contact support')
}
});
});
Route
Route::post('/save_edit/{id}','QuotationController#save_quote_edited');
Controller
public function save_quote_edited(Request $request){
dd(json_decode($request->getContent(), true));
}
To process forms you should not be calling $request->getContent() you should be calling $request->all() to get all parameters, $request->get('foo') or $request->foo to get a single parameter, or $request->get(['foo','bar','baz') to get multiple parameters.
And for bonus points if you used Laravel's Form Request Validation you could call $request->validated() and get only the parameters which passed the validation checks.
Your route requires a parameter of "id" but you do not have that parameter in your controller method.
Route::post('/save_edit/{id}','QuotationController#save_quote_edited');
Try :
public function save_quote_edited(Request $request, $id){
*** insert code here ***
}
Guys i have made a blog using Laravel framework, today I just heard about ajax, What I heard is in short: it loads data quickly. My issue is that I have many routes , controllers with views.
What steps do i need to use so called ajax javascript?
<script>
$.ajaxSetup({
headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content')
}
});
jQuery.ajax({
url:'/blog',
type: 'GET',
success: getIndex( ){
console.log( );
},
});
</script>
It is basically the same, when you make a HTTP request using ajax, you need to define the route you are requesting, for example this ajax request:
$.ajax({
type: 'post',
url: 'your/url',
dataType: 'json',
success: function (data) {
}
});
You define your route like a normal route would be, since this ajax request is type "post", you define your route as "post":
Route::post('your/url', 'yourController#yourFunctionInsideController')->name('your.route.alias');
Ajax can be used normally in Laravel, I particularly usually host my codes in '/ public / js' and then extend through the <script> so that the code is not mixed. I advise you to study Laravel's structure, read the documentation, see some videos. Your question is very broad, so I will leave some materials that are of interest to you.
Book by Novatec about Ajax:
https://www.novatec.com.br/livros/ajaxjquery/
Laravel Docs:
https://laravel.com/docs/5.4/
I am working on Jquery AJAX in OXID eSHOP.
I want to pass proper action (function) name in AJAX url parameter.
Below is my code:
jQuery.ajax({
type: "POST",
data: {
variable: value
},
url: "",
success: function(data) {
}
});
I want to call one function in url parameter.
But don't know how to pass its value in url paramaeter.
Anyone knows then please help me.
You should include function name as URL parameter, just like the shop does it in most situations:
http://your-shop.com/index.php?cl=controller&fnc=functionname
Is it possible to make AJAX PATCH requests to laravel, or am I restricted to POST? Laravel uses PATCH in input hidden fields, however, I am not using form elements—just buttons that should partially update a record when clicked (via an AJAX request).
How would the route look like for this?
Routes file
Route::patch('questions/{id}', 'QuestionController#update')->before('admin');
I am not sure if laravel routes support PATCH.
Controller
public function update($id) {
if (Request::ajax() && Request::isMethod('patch')) {
//partially update record here
}
}
JS
$('div#question_preview <some button selector>').click(function (event) {
$.ajax({
url: 'questions/'+question_id,
type: 'PATCH',
data: {status: 'some status'}
});
});
I am just looking for clarity, thanks!
Yeah, it's possible try
In Your JavaScript
$('#div#question_preview <some button selector>').click(function() {
$.ajax({
url: 'questions/'+question_id,
type: 'PATCH',
data: {status: <SOME VALUE I WANT>, _method: "PATCH"},
success: function(res) {
}
});
});
In Your Route
Route::patch('questions/{id}', 'QuestionController#update')->before('admin');
In your QuestionController Controller's update method
dd(Request::method());
You will see the respond like
string(5) "PATCH"
Read more about Request Information on Laravel doc.
It's possible!
You should need to provide an extra parameter in the form request called as _method with a value PATCH.
JS
$('div#question_preview <some button selector>').click(function (event) {
$.ajax({
url: 'questions/'+question_id,
type: 'PATCH',
data: {status: 'some status',_method: 'PATCH'}
});
});
You can provide a hidden input in the view file with the value PATCH for better readability
HTML
<input type="hidden" name="_method" value="PATCH">
If you are using FormData, you need to send the request as POST. The Laravel application will automatically get it as a PATCH request because you included #method('PATCH'). So your route and method for that will get triggered.
JS with FormData
$('div#question_preview <some button selector>').click(function (event) {
let form_data= new FormData();
form_data.append('status','some status');
form_data.append('_method','PATCH');
$.ajax({
url: 'questions/'+question_id,
type: 'POST',
data: form_data
});
});
I'm trying to use ajax with Codeigniter, but I'm getting a 404 (not found) error.
For ajax I do this:
$.ajax({
type: "POST",
url: "/index.php/ajax/user-sign-up-via-email",
data: {
email: email,
password: password
}
});
For my Routes.php I have this:
$route['ajax/user-sign-up-via-email'] = "UserSignUpViaEmailAjaxController";
Am I missing something specific to Codeigniter?
Is it related to first URL parameter being the controller, second parameter being the function to call within the controller?
As per comments:
Make sure that the controller you are calling (UserSignUpViaEmailAjaxController) has an index() function. Since you are remapping the uri directly to a controller without specifying a function it will default to the index() function and will 404 if it can't find one.
i do this normally .. u can try this also
url: "<?php echo site_url('customersController/addCustomer'); ?>",