简体   繁体   English

Ajax 在 laravel 中返回未定义

[英]Ajax returning undefined in laravel

First of all, I am a beginner in using ajax inside laravel.首先,我是在 laravel 中使用 ajax 的初学者。 I tried to check the input field while it's working or not through an alert or console.log it's returning undefined.我尝试在输入字段工作时检查输入字段,或者通过它返回未定义的警报或 console.log 来检查它。

my blade file code我的刀片文件代码

<div id="add_tag_modal" class="modal fade">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body">
                <h2>Add New Tag</h2>
                <hr>
                <form id="add_tag_form" method="POST">
                    @csrf
                    <div class="mess"></div>
                    <div class="form-group">
                        <label for="">Name</label>
                        <input name="name" type="text" class="form-control">
                    </div>
                    <div class="form-group">
                        <input class="btn btn-primary btn-sm" type="submit" >
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

my controller file code..though its commented public function store(Request $request) { // Tag::create([我的 controller 文件代码..虽然它的评论是公开的 function 存储(请求 $request){ // Tag::create([

    //     'name' => $request -> name

    // ]);
}

web.php /** web.php /**

  • tag controller */标签 controller */

Route::resource('tag','App\Http\Controllers\TagController'); Route::resource('tag','App\Http\Controllers\TagController');

Route::post('tag-create','App\Http\Controllers\TagController@store')->name('tag.create'); Route::post('tag-create','App\Http\Controllers\TagController@store')->name('tag.create');

lastly my ajax code最后是我的 ajax 代码

  $(document).on('submit','form#add_tag_form',function(e){

    e.preventDefault();


    let name = $('form#add_tag_form input[name="name"]').val();


    if (name == '') {

        $('.mess').html('<p class="alert alert-danger">All fields are required!<button type="button" class="close" data-dismiss="alert" aria-label="Close">');

    }else{

         $.ajax({
    url:'tag-create',
    method: "POST",
    contentType: false,
    processData : false,
    data : new FormData(this),
    success: function(data){
        console.log(data.name);

    }
         });
    }

  });

I believe you need to update your controller.我相信您需要更新您的 controller。

You need to validate the request first您需要先验证请求

$validate = $this->validate($request, [
    'name' => 'required|string|max:255'
]);

after that, send the $validate variable to model Tag to create a new resource:之后,将$validate变量发送到 model 标记以创建新资源:

$save = Tag::create($validate);

if($save) {
   return true; //return statements you can customize based on your requirement
} 
return false;

By doing this you are validating the incoming request and if your return value is true process further.通过这样做,您正在验证传入的请求,并且您的返回值是否为 true 会进一步处理。 But be carefull with response you should return the message and status code to validate the response.. Don't use true or false for return statement.但是要小心响应,您应该返回消息和状态代码以验证响应。不要使用 true 或 false 作为 return 语句。 In AJAX response, you check the status code and proceed further with the message.在 AJAX 响应中,您检查状态代码并继续处理消息。

Thank you谢谢

You should try to replace 'let name' into 'var name' then alert or console it.您应该尝试将 'let name' 替换为 'var name' 然后警告或控制台它。

You can try adding this to your php code to show the error message of the 500 code.您可以尝试将此添加到您的 php 代码中以显示 500 代码的错误消息。

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

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

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