Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php URL::操作在jquery ajax中不起作用_Php_Jquery_Ajax_Laravel_Laravel 4 - Fatal编程技术网

Php URL::操作在jquery ajax中不起作用

Php URL::操作在jquery ajax中不起作用,php,jquery,ajax,laravel,laravel-4,Php,Jquery,Ajax,Laravel,Laravel 4,我正在使用Laravel4.2,并试图使用ajax更新数据,但在ajax重定向url中,我无法使用。因为我使用的是URL:action-like $.ajax({ type: 'POST', dataType: 'json', url : "store", data: { title: postTitle['post-title']['value'], body: postContent['post-body']['value'] }, success:

我正在使用Laravel4.2,并试图使用ajax更新数据,但在ajax重定向url中,我无法使用。因为我使用的是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...');
    }
});

我不知道为什么它不起作用。请帮助我。

在路线文件中添加路线:

Route::get('post','PostsController@index');
将js更改为:

 setTimeout(function() {
                window.location = "<?php echo URL::action('PostsController@index') ?>";
            }, 2000);
setTimeout(函数(){
window.location=“”;
}, 2000);
或:

setTimeout(函数(){
window.location=“”;
}, 2000);

您需要在路由文件中添加路由:

Route::post('post', 'PostsController@index');
但是如果您启用了CSRF,那么您还需要发布CSRF代码。你可以通过在你的帖子“数据”中添加这一点来做到这一点


我希望这对你有用

你在这里干什么真是个可怕的习惯。如果您有选择的话,您永远不应该在真实的应用程序中使用动态创建的JS代码

首先,您需要将JS和PHP代码紧密耦合(有点反MVC)。请求时间增加。维护应用程序更难。您不能使用准备好的(缩小的)JS等

在这里,您应该手动创建URL:

window.location.href = "/post/something";
只需创建路由并使用它,而无需
URL::

Route::post('post/something', 'PostsController@postSomething');

注意:您需要到该控制器的路由action@madalinivascu如何在路由中添加控制器操作在这种情况下它不起作用,它返回url,如
http://localhost/editorlara4/public/posts/%7B%7B%20URL::action%28'PostsController@index“%29%20%7D%7D
模板上是否已启用刀片服务器?我不知道刀片服务器是否已启用。如何检查其启用与否您的视图文件应该以他的名字命名blade嗯,是的,madalin我的视图文件带有.blade.php扩展名。如果他想更改控制器操作的路径,会发生什么情况?url会发生什么情况?为什么您称之为糟糕的做法?他会手动更改url。更新后的帖子告诉我们为什么这是一个坏习惯。
window.location.href = "/post/something";
Route::post('post/something', 'PostsController@postSomething');