Reputation: 604
Am using laravel 4.2 and trying to update data using ajax but in ajax redirect url not working for me. Because am using URL:action like,
$.ajax({
type: 'POST',
dataType: 'json',
url : "store",
data: { title: postTitle['post-title']['value'], body: postContent['post-body']['value'] },
success: function(data) {
if(data.success === false)
{
$('.error').append(data.message);
$('.error').show();
} else {
$('.success').append(data.message);
$('.success').show();
setTimeout(function() {
window.location.href = "{{ URL::action('PostsController@index') }}";
}, 2000);
}
},
error: function(xhr, textStatus, thrownError) {
alert('Something went wrong. Please Try again later...');
}
});
i dont know why its not working. please help me.
Upvotes: 4
Views: 743
Reputation: 163798
What are you doing here is a really terrible practice. You should never use dynamically created JS code in a real app, if you have a choice.
First of all, you're tight coupling JS and PHP code (kind of anti-MVC). Request time increases. It's harder to maintain the app. You are not able to use prepared (minified) JS etc.
Why generating JS with PHP is a bad practice
Here, you should create URL manually:
window.location.href = "/post/something";
Just create route and use it without URL::
Route::post('post/something', 'PostsController@postSomething');
Upvotes: 1
Reputation: 32354
Add a route in your route file:
Route::get('post','PostsController@index');
change js to:
setTimeout(function() {
window.location = "<?php echo URL::action('PostsController@index') ?>";
}, 2000);
or:
setTimeout(function() {
window.location = "<?php echo action('PostsController@index') ?>";
}, 2000);
Upvotes: 0
Reputation: 3422
You need to add a route in your routes file:
Route::post('post', 'PostsController@index');
But if you enabled CSRF, then you also need to post the CSRF code. You can do this through add this in your post "data".
...
url : "{{ url('store') }}",
Data: data: { title: postTitle['post-title']['value'], body: postContent['post-body']['value'] }, _token: {{ csrf_token() }},
...
I hoped this works for you!
Upvotes: 2