简体   繁体   English

使用laravel验证后,对ajax的成功函数

[英]success function on ajax after validation with laravel

I made the validation of a form with laravel through ajax. 我通过ajax用laravel验证了一个表单。 sending data and validation work properly, but when the answer in the js code is received, the success function is never executed; 正确地发送数据和验证工作,但是当收到js代码中的答案时,永远不会执行成功函数; if there are errors in the validation error function is executed and if there are no errors in the validation function also enters the error. 如果验证错误中存在错误,则执行错误功能,如果验证功能中没有错误,也会输入错误。 On error, it returns status laravel: 422 and if no errors, returns laravel status: 200. 出错时,它返回状态laravel:422,如果没有错误,则返回laravel status:200。

Thank you for the help that you may be able to provide :) 感谢您提供的帮助:)

js: JS:

function send(event){

    event.preventDefault();

    $.ajax({


        type: 'post',
        url: userAjax,
        dataType: 'json',
        data: {
            name: $('#name').val(),
            email: $('#email').val(),
            password: $('#password').val(),
            cpassword: $('#cpassword').val(),
            _token: $('#new-user').attr('data-token')
        },
        beforeSend: function()
        {
            $("#nameError").fadeOut();

        },
        success: function(errors){

            $('#Register').modal('hide');

        },
        error: function(errors) {


            console.log(errors);

        }

    });

}

laravel: UserController: laravel:UserController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Requests\UserRequest;

class UserController extends Controller
{
    public function index()
    {
        return view('home');
    }
    public function NewUser(UserRequest $request)
    {
        // laravel will validate request itself automatically and
        // will go on if validate pass or will return back with
        // status code 422, input and errors otherwise

        CreateNewUser($request);

    }

    Protected function CreateNewUser(Request $request){

    }


}

UserRequest: UserRequest:

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class UserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
        public function rules()
    {
        return [
            'name' => 'required|max:255',
//            'email' => 'required|email|max:255|unique:users',
//            'password' => 'required|min:8|same:cpassword',
//            'cpassword'=> 'required|min:8',
        ];
    }

}

It's because you have errors in your code. 这是因为你的代码中有错误。

CreateNewUser($request);

Needs to be 需要是

$this->CreateNewUser($request);

And

Protected function CreateNewUser(Request $request){

needs to be: 需要是:

protected function CreateNewUser(Request $request){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM