Problems with Ajax-request via Laravel
up vote
0
down vote
favorite
I have a big problem with Ajax-request via Laravel. I can't understand why it is not Ajax. All resources here.
It is my Ajax-request.
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var nickname = "<?php echo $name_user; ?>";
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.subscribe').click(function() {
jQuery.ajax({
type: 'post',
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}", //Путь к обработчик
data: {'user_name': nickname},
response: 'text',
success: function(data) {
console.log(data['result']);
},
error: alert('Error');
})
})
</script>
It is my web-routes.
Route::group(['middleware' => 'auth'], function() {
Route::get('/', 'AccountController@redirectToAccountPage');
Route::get('/account', 'AccountController@showAccount')->name('account');
Route::get('/account/settings', 'AccountController@showSettings')->name('settings');
Route::post('/account/settings', 'AccountController@sendSetting');
Route::get('/account/subscribe', 'AccountController@showSubscriberForm')->name('subscriber');
Route::post('/account/subscribe', 'AccountController@findUser')->name('postFindUser');
Route::get('/account/load_image', 'AccountController@showLoadImage')->name('load_image');
Route::post('/account/load_image', 'PhotosLoadPhotoController@loadPhoto');
Route::post('/account/logout', 'AccountController@logout')->name('logout');
Route::post('/user/{user_name}/', 'AccountController@subscribe')->name('postSubscribe');
Route::get('/admin', 'AccountController@showAdminPanel');
});
It's my main request.
public function subscribe(Request $request, $name_user) {
if($request->ajax()) {
$query = 'SELECT id FROM subscriptions WHERE id_subscriber = ? AND id_subscribtion = ?';
$queryFindAnother = 'SELECT id From new_users WHERE nickname = ?';
$idAnotherUser = DB::select($queryFindAnother, [$name_user]);
if(!DB::select($query, [Auth::user()->id, $idAnotherUser[0]->id])) {
$query = 'INSERT INTO subscriptions (id_subscriber, id_subscribtion) VALUES (?, ?)';
dd('Я здесь');
//id_subscriber - тот, кто подписался.
//id_subscribtion - на кого подписан.
DB::insert($query, [Auth::user()->id, $idAnotherUser[0]->id]);
return response()->json([
'result' => '1', //всё прошло успешно, я подписан
]);
}
return back();
}
dd("It's not ajax");
return back();
}
As a result I got the message "It's not Ajax". Help me please!
javascript php laravel logic
add a comment |
up vote
0
down vote
favorite
I have a big problem with Ajax-request via Laravel. I can't understand why it is not Ajax. All resources here.
It is my Ajax-request.
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var nickname = "<?php echo $name_user; ?>";
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.subscribe').click(function() {
jQuery.ajax({
type: 'post',
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}", //Путь к обработчик
data: {'user_name': nickname},
response: 'text',
success: function(data) {
console.log(data['result']);
},
error: alert('Error');
})
})
</script>
It is my web-routes.
Route::group(['middleware' => 'auth'], function() {
Route::get('/', 'AccountController@redirectToAccountPage');
Route::get('/account', 'AccountController@showAccount')->name('account');
Route::get('/account/settings', 'AccountController@showSettings')->name('settings');
Route::post('/account/settings', 'AccountController@sendSetting');
Route::get('/account/subscribe', 'AccountController@showSubscriberForm')->name('subscriber');
Route::post('/account/subscribe', 'AccountController@findUser')->name('postFindUser');
Route::get('/account/load_image', 'AccountController@showLoadImage')->name('load_image');
Route::post('/account/load_image', 'PhotosLoadPhotoController@loadPhoto');
Route::post('/account/logout', 'AccountController@logout')->name('logout');
Route::post('/user/{user_name}/', 'AccountController@subscribe')->name('postSubscribe');
Route::get('/admin', 'AccountController@showAdminPanel');
});
It's my main request.
public function subscribe(Request $request, $name_user) {
if($request->ajax()) {
$query = 'SELECT id FROM subscriptions WHERE id_subscriber = ? AND id_subscribtion = ?';
$queryFindAnother = 'SELECT id From new_users WHERE nickname = ?';
$idAnotherUser = DB::select($queryFindAnother, [$name_user]);
if(!DB::select($query, [Auth::user()->id, $idAnotherUser[0]->id])) {
$query = 'INSERT INTO subscriptions (id_subscriber, id_subscribtion) VALUES (?, ?)';
dd('Я здесь');
//id_subscriber - тот, кто подписался.
//id_subscribtion - на кого подписан.
DB::insert($query, [Auth::user()->id, $idAnotherUser[0]->id]);
return response()->json([
'result' => '1', //всё прошло успешно, я подписан
]);
}
return back();
}
dd("It's not ajax");
return back();
}
As a result I got the message "It's not Ajax". Help me please!
javascript php laravel logic
please add the element of HTML which is associated with .subscribe class
– Emtiaz Zahid
Nov 20 at 3:17
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}",
... this looks like you mixing things up. Try just{{ route('postSubscribe', $name_user) }}",
instead.
– Peter
Nov 20 at 3:53
@emtiaz-zahid, it already exists. <form method="post" action=""> {!! csrf_field() !!} <button class="subscribe" name="user">Подписаться.</button> </form> Maybe my problem can be connected with empty action but I have a route...
– Егор Коротцев
Nov 20 at 4:12
@peter, your way to solve my problem is not working. Try to help me again. Thank you for your previous way.
– Егор Коротцев
Nov 20 at 4:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a big problem with Ajax-request via Laravel. I can't understand why it is not Ajax. All resources here.
It is my Ajax-request.
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var nickname = "<?php echo $name_user; ?>";
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.subscribe').click(function() {
jQuery.ajax({
type: 'post',
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}", //Путь к обработчик
data: {'user_name': nickname},
response: 'text',
success: function(data) {
console.log(data['result']);
},
error: alert('Error');
})
})
</script>
It is my web-routes.
Route::group(['middleware' => 'auth'], function() {
Route::get('/', 'AccountController@redirectToAccountPage');
Route::get('/account', 'AccountController@showAccount')->name('account');
Route::get('/account/settings', 'AccountController@showSettings')->name('settings');
Route::post('/account/settings', 'AccountController@sendSetting');
Route::get('/account/subscribe', 'AccountController@showSubscriberForm')->name('subscriber');
Route::post('/account/subscribe', 'AccountController@findUser')->name('postFindUser');
Route::get('/account/load_image', 'AccountController@showLoadImage')->name('load_image');
Route::post('/account/load_image', 'PhotosLoadPhotoController@loadPhoto');
Route::post('/account/logout', 'AccountController@logout')->name('logout');
Route::post('/user/{user_name}/', 'AccountController@subscribe')->name('postSubscribe');
Route::get('/admin', 'AccountController@showAdminPanel');
});
It's my main request.
public function subscribe(Request $request, $name_user) {
if($request->ajax()) {
$query = 'SELECT id FROM subscriptions WHERE id_subscriber = ? AND id_subscribtion = ?';
$queryFindAnother = 'SELECT id From new_users WHERE nickname = ?';
$idAnotherUser = DB::select($queryFindAnother, [$name_user]);
if(!DB::select($query, [Auth::user()->id, $idAnotherUser[0]->id])) {
$query = 'INSERT INTO subscriptions (id_subscriber, id_subscribtion) VALUES (?, ?)';
dd('Я здесь');
//id_subscriber - тот, кто подписался.
//id_subscribtion - на кого подписан.
DB::insert($query, [Auth::user()->id, $idAnotherUser[0]->id]);
return response()->json([
'result' => '1', //всё прошло успешно, я подписан
]);
}
return back();
}
dd("It's not ajax");
return back();
}
As a result I got the message "It's not Ajax". Help me please!
javascript php laravel logic
I have a big problem with Ajax-request via Laravel. I can't understand why it is not Ajax. All resources here.
It is my Ajax-request.
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var nickname = "<?php echo $name_user; ?>";
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.subscribe').click(function() {
jQuery.ajax({
type: 'post',
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}", //Путь к обработчик
data: {'user_name': nickname},
response: 'text',
success: function(data) {
console.log(data['result']);
},
error: alert('Error');
})
})
</script>
It is my web-routes.
Route::group(['middleware' => 'auth'], function() {
Route::get('/', 'AccountController@redirectToAccountPage');
Route::get('/account', 'AccountController@showAccount')->name('account');
Route::get('/account/settings', 'AccountController@showSettings')->name('settings');
Route::post('/account/settings', 'AccountController@sendSetting');
Route::get('/account/subscribe', 'AccountController@showSubscriberForm')->name('subscriber');
Route::post('/account/subscribe', 'AccountController@findUser')->name('postFindUser');
Route::get('/account/load_image', 'AccountController@showLoadImage')->name('load_image');
Route::post('/account/load_image', 'PhotosLoadPhotoController@loadPhoto');
Route::post('/account/logout', 'AccountController@logout')->name('logout');
Route::post('/user/{user_name}/', 'AccountController@subscribe')->name('postSubscribe');
Route::get('/admin', 'AccountController@showAdminPanel');
});
It's my main request.
public function subscribe(Request $request, $name_user) {
if($request->ajax()) {
$query = 'SELECT id FROM subscriptions WHERE id_subscriber = ? AND id_subscribtion = ?';
$queryFindAnother = 'SELECT id From new_users WHERE nickname = ?';
$idAnotherUser = DB::select($queryFindAnother, [$name_user]);
if(!DB::select($query, [Auth::user()->id, $idAnotherUser[0]->id])) {
$query = 'INSERT INTO subscriptions (id_subscriber, id_subscribtion) VALUES (?, ?)';
dd('Я здесь');
//id_subscriber - тот, кто подписался.
//id_subscribtion - на кого подписан.
DB::insert($query, [Auth::user()->id, $idAnotherUser[0]->id]);
return response()->json([
'result' => '1', //всё прошло успешно, я подписан
]);
}
return back();
}
dd("It's not ajax");
return back();
}
As a result I got the message "It's not Ajax". Help me please!
javascript php laravel logic
javascript php laravel logic
asked Nov 20 at 2:57
Егор Коротцев
11
11
please add the element of HTML which is associated with .subscribe class
– Emtiaz Zahid
Nov 20 at 3:17
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}",
... this looks like you mixing things up. Try just{{ route('postSubscribe', $name_user) }}",
instead.
– Peter
Nov 20 at 3:53
@emtiaz-zahid, it already exists. <form method="post" action=""> {!! csrf_field() !!} <button class="subscribe" name="user">Подписаться.</button> </form> Maybe my problem can be connected with empty action but I have a route...
– Егор Коротцев
Nov 20 at 4:12
@peter, your way to solve my problem is not working. Try to help me again. Thank you for your previous way.
– Егор Коротцев
Nov 20 at 4:18
add a comment |
please add the element of HTML which is associated with .subscribe class
– Emtiaz Zahid
Nov 20 at 3:17
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}",
... this looks like you mixing things up. Try just{{ route('postSubscribe', $name_user) }}",
instead.
– Peter
Nov 20 at 3:53
@emtiaz-zahid, it already exists. <form method="post" action=""> {!! csrf_field() !!} <button class="subscribe" name="user">Подписаться.</button> </form> Maybe my problem can be connected with empty action but I have a route...
– Егор Коротцев
Nov 20 at 4:12
@peter, your way to solve my problem is not working. Try to help me again. Thank you for your previous way.
– Егор Коротцев
Nov 20 at 4:18
please add the element of HTML which is associated with .subscribe class
– Emtiaz Zahid
Nov 20 at 3:17
please add the element of HTML which is associated with .subscribe class
– Emtiaz Zahid
Nov 20 at 3:17
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}",
... this looks like you mixing things up. Try just {{ route('postSubscribe', $name_user) }}",
instead.– Peter
Nov 20 at 3:53
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}",
... this looks like you mixing things up. Try just {{ route('postSubscribe', $name_user) }}",
instead.– Peter
Nov 20 at 3:53
@emtiaz-zahid, it already exists. <form method="post" action=""> {!! csrf_field() !!} <button class="subscribe" name="user">Подписаться.</button> </form> Maybe my problem can be connected with empty action but I have a route...
– Егор Коротцев
Nov 20 at 4:12
@emtiaz-zahid, it already exists. <form method="post" action=""> {!! csrf_field() !!} <button class="subscribe" name="user">Подписаться.</button> </form> Maybe my problem can be connected with empty action but I have a route...
– Егор Коротцев
Nov 20 at 4:12
@peter, your way to solve my problem is not working. Try to help me again. Thank you for your previous way.
– Егор Коротцев
Nov 20 at 4:18
@peter, your way to solve my problem is not working. Try to help me again. Thank you for your previous way.
– Егор Коротцев
Nov 20 at 4:18
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I think the problem is in your form
Please update your form
<form>
{!! csrf_field() !!}
<button class="subscribe" name="user" type="button">Подписаться.</button>
</form>
Here, you don't need any action or method also does not need any form
because you passing the form data as ajax
and add the type of your button as button
, by default its a submit type, so its submitting the form as normally.
I changed the button into the input with type = submit but it doesn't work. Thanks anyway for your solution. Maybe the problem can be connected with X-CSRF-TOKEN when I send the Ajax-request.
– Егор Коротцев
Nov 20 at 5:31
add a comment |
up vote
0
down vote
Make sure you have imported the Request Facade on top
Thank you for your solution but it doesn't work.
– Егор Коротцев
Nov 20 at 5:27
I added "use IlluminateSupportFacades" at the top of the page where there is Ajax-request and where I have my main request but it doesn't work. Thanks anyway for your solution.
– Егор Коротцев
Nov 20 at 5:38
use IlluminateHttpRequest; tried this?
– Ravi
Nov 20 at 6:08
Yes, I tried. It doesn't work too. Thanks anyway.
– Егор Коротцев
Nov 20 at 7:24
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I think the problem is in your form
Please update your form
<form>
{!! csrf_field() !!}
<button class="subscribe" name="user" type="button">Подписаться.</button>
</form>
Here, you don't need any action or method also does not need any form
because you passing the form data as ajax
and add the type of your button as button
, by default its a submit type, so its submitting the form as normally.
I changed the button into the input with type = submit but it doesn't work. Thanks anyway for your solution. Maybe the problem can be connected with X-CSRF-TOKEN when I send the Ajax-request.
– Егор Коротцев
Nov 20 at 5:31
add a comment |
up vote
0
down vote
I think the problem is in your form
Please update your form
<form>
{!! csrf_field() !!}
<button class="subscribe" name="user" type="button">Подписаться.</button>
</form>
Here, you don't need any action or method also does not need any form
because you passing the form data as ajax
and add the type of your button as button
, by default its a submit type, so its submitting the form as normally.
I changed the button into the input with type = submit but it doesn't work. Thanks anyway for your solution. Maybe the problem can be connected with X-CSRF-TOKEN when I send the Ajax-request.
– Егор Коротцев
Nov 20 at 5:31
add a comment |
up vote
0
down vote
up vote
0
down vote
I think the problem is in your form
Please update your form
<form>
{!! csrf_field() !!}
<button class="subscribe" name="user" type="button">Подписаться.</button>
</form>
Here, you don't need any action or method also does not need any form
because you passing the form data as ajax
and add the type of your button as button
, by default its a submit type, so its submitting the form as normally.
I think the problem is in your form
Please update your form
<form>
{!! csrf_field() !!}
<button class="subscribe" name="user" type="button">Подписаться.</button>
</form>
Here, you don't need any action or method also does not need any form
because you passing the form data as ajax
and add the type of your button as button
, by default its a submit type, so its submitting the form as normally.
answered Nov 20 at 4:22
Emtiaz Zahid
1,025516
1,025516
I changed the button into the input with type = submit but it doesn't work. Thanks anyway for your solution. Maybe the problem can be connected with X-CSRF-TOKEN when I send the Ajax-request.
– Егор Коротцев
Nov 20 at 5:31
add a comment |
I changed the button into the input with type = submit but it doesn't work. Thanks anyway for your solution. Maybe the problem can be connected with X-CSRF-TOKEN when I send the Ajax-request.
– Егор Коротцев
Nov 20 at 5:31
I changed the button into the input with type = submit but it doesn't work. Thanks anyway for your solution. Maybe the problem can be connected with X-CSRF-TOKEN when I send the Ajax-request.
– Егор Коротцев
Nov 20 at 5:31
I changed the button into the input with type = submit but it doesn't work. Thanks anyway for your solution. Maybe the problem can be connected with X-CSRF-TOKEN when I send the Ajax-request.
– Егор Коротцев
Nov 20 at 5:31
add a comment |
up vote
0
down vote
Make sure you have imported the Request Facade on top
Thank you for your solution but it doesn't work.
– Егор Коротцев
Nov 20 at 5:27
I added "use IlluminateSupportFacades" at the top of the page where there is Ajax-request and where I have my main request but it doesn't work. Thanks anyway for your solution.
– Егор Коротцев
Nov 20 at 5:38
use IlluminateHttpRequest; tried this?
– Ravi
Nov 20 at 6:08
Yes, I tried. It doesn't work too. Thanks anyway.
– Егор Коротцев
Nov 20 at 7:24
add a comment |
up vote
0
down vote
Make sure you have imported the Request Facade on top
Thank you for your solution but it doesn't work.
– Егор Коротцев
Nov 20 at 5:27
I added "use IlluminateSupportFacades" at the top of the page where there is Ajax-request and where I have my main request but it doesn't work. Thanks anyway for your solution.
– Егор Коротцев
Nov 20 at 5:38
use IlluminateHttpRequest; tried this?
– Ravi
Nov 20 at 6:08
Yes, I tried. It doesn't work too. Thanks anyway.
– Егор Коротцев
Nov 20 at 7:24
add a comment |
up vote
0
down vote
up vote
0
down vote
Make sure you have imported the Request Facade on top
Make sure you have imported the Request Facade on top
answered Nov 20 at 4:32
Ravi
1
1
Thank you for your solution but it doesn't work.
– Егор Коротцев
Nov 20 at 5:27
I added "use IlluminateSupportFacades" at the top of the page where there is Ajax-request and where I have my main request but it doesn't work. Thanks anyway for your solution.
– Егор Коротцев
Nov 20 at 5:38
use IlluminateHttpRequest; tried this?
– Ravi
Nov 20 at 6:08
Yes, I tried. It doesn't work too. Thanks anyway.
– Егор Коротцев
Nov 20 at 7:24
add a comment |
Thank you for your solution but it doesn't work.
– Егор Коротцев
Nov 20 at 5:27
I added "use IlluminateSupportFacades" at the top of the page where there is Ajax-request and where I have my main request but it doesn't work. Thanks anyway for your solution.
– Егор Коротцев
Nov 20 at 5:38
use IlluminateHttpRequest; tried this?
– Ravi
Nov 20 at 6:08
Yes, I tried. It doesn't work too. Thanks anyway.
– Егор Коротцев
Nov 20 at 7:24
Thank you for your solution but it doesn't work.
– Егор Коротцев
Nov 20 at 5:27
Thank you for your solution but it doesn't work.
– Егор Коротцев
Nov 20 at 5:27
I added "use IlluminateSupportFacades" at the top of the page where there is Ajax-request and where I have my main request but it doesn't work. Thanks anyway for your solution.
– Егор Коротцев
Nov 20 at 5:38
I added "use IlluminateSupportFacades" at the top of the page where there is Ajax-request and where I have my main request but it doesn't work. Thanks anyway for your solution.
– Егор Коротцев
Nov 20 at 5:38
use IlluminateHttpRequest; tried this?
– Ravi
Nov 20 at 6:08
use IlluminateHttpRequest; tried this?
– Ravi
Nov 20 at 6:08
Yes, I tried. It doesn't work too. Thanks anyway.
– Егор Коротцев
Nov 20 at 7:24
Yes, I tried. It doesn't work too. Thanks anyway.
– Егор Коротцев
Nov 20 at 7:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385564%2fproblems-with-ajax-request-via-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please add the element of HTML which is associated with .subscribe class
– Emtiaz Zahid
Nov 20 at 3:17
url: "{{ route('postSubscribe', '<?php echo $name_user; ?>') }}",
... this looks like you mixing things up. Try just{{ route('postSubscribe', $name_user) }}",
instead.– Peter
Nov 20 at 3:53
@emtiaz-zahid, it already exists. <form method="post" action=""> {!! csrf_field() !!} <button class="subscribe" name="user">Подписаться.</button> </form> Maybe my problem can be connected with empty action but I have a route...
– Егор Коротцев
Nov 20 at 4:12
@peter, your way to solve my problem is not working. Try to help me again. Thank you for your previous way.
– Егор Коротцев
Nov 20 at 4:18