0
我通過ajax對laravel的表單進行了驗證。發送數據和驗證工作正常,但是當接收到js代碼中的答案時,成功函數從不執行;如果驗證錯誤中存在錯誤,則執行驗證功能並且如果驗證功能中沒有錯誤,則也輸入錯誤。 錯誤時,它返回狀態laravel:422,如果沒有錯誤,返回laravel狀態:200ajax在laravel驗證後的成功函數
謝謝你的幫助,你可能能夠提供:)
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中:
<?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:
<?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',
];
}
}
喜@Ohgodwhy,謝謝你糾正錯誤,但功能創建新用戶在驗證後運行。我糾正並做了測試,但仍然進入了錯誤ajax函數。 –