在ajax请求后返回视图或重定向

时间:2017-01-02 08:03:22

标签: php jquery ajax laravel redirect

如何无法重定向o在ajax请求后返回视图。

我尝试验证请求并在之后(如果请求正常)返回一个视图,但这不起作用,我尝试重定向到新路由但是也不行。

这是我的控制者:

    function selectDate(){
    $(document).ready(function () {
        $( "#datepicker" ).datepicker({ dateFormat: 'dd-mm-yy' });
    });
}

function searchActivity(event) {
    event.preventDefault();

    console.log(event);
    $.ajax({
        type: 'POST',
        url: searchRoute,
        data: {
            search: $('.search').val(),
            date: $('#datepicker').val(),
            _token: $('#token').val()
        },

        dataType : "json",

        success: function (data) {
            if (data.success){

              console.log(data)

            }else{
                console.log(data.errors);

            }
        }
    })
}

JS代码:

.filter()

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

您需要更改您的PHP代码

if ($validator->fails()){

    return response()->json([
        'sucess' => false,
        'errors' => $validator->errors()->toArray(),
        'data' => $request->all()
    ]);
}else{
    return response()->json([
        'sucess' => true,
        'what_you_want' => ''
    ]);    }

像这样更改你的ajax代码

  success: function (data) {
        if (data.success){

          //console.log(data)
         window.location.replace("http://stackoverflow.com");

        }else{
            console.log(data.errors);
            window.location.replace("http://google.com");
        }
    }

答案 1 :(得分:0)

在成功功能中使用import { Component } from '@angular/core'; import { Title } from '@angular/platform-browser'; @Component({ selector: 'my-app', template: `<p> Select a title to set on the current HTML document: </p> <ul> <li><a (click)="setTitle( 'Good morning!' )">Good morning</a>.</li> <li><a (click)="setTitle( 'Good afternoon!' )">Good afternoon</a>.</li> <li><a (click)="setTitle( 'Good evening!' )">Good evening</a>.</li> </ul> ` }) export class AppComponent { public constructor(private titleService: Title ) { } public setTitle( newTitle: string) { this.titleService.setTitle( newTitle ); } } header("url)