dongzhiyan5693 2016-12-17 23:19
浏览 67
已采纳

使用javascript和Ajax POST形成JSON [LARAVEL]

I have to pass form data with POST (converting it into JSON format), exploiting Javascript and Ajax in Laravel.

Basically, the data in the form have to became a json in order to pass it (with POST), to Controller class with a method able to uses the data. I have a bootstrap form:

 <form id="contactForm" action="#" method="post">
<div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>

<div class="form-group">
    <label for="exampleTextarea">Example textarea</label>
    <textarea class="form-control" id="exampleTextarea" rows="3"></textarea>
</div>

<button type="submit" class="btn btn-primary">Submit</button>
</form>

Now, to pass the data converting it in JSON I used:

 <script>
 var $contactForm = $('#contactForm');
 $contactForm.submit(function(e) {
 e.preventDefault();
 $.ajax({
    url: './getContact',
    method: 'POST',
    data: $(this).serialize(),
    dataType: 'json',
success:success: function(data)
{

}
});
});
</script>

Exploiting Laravel routes.php

Route::post('./getContact', 'Controller@tryIt');

It is a correct way to use this service? (This due to the fact that I can't use the data form, like the POST doesn't provide any success).

Could you help me?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • douniani679741 2016-12-17 23:51
    关注

    First you need to add the csrf token Also try this way.

    HTML

    <form id="contactForm" action="#" method="post">
    {!! csrf_field() !!}
    <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
        <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
    </div>
    <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
    </div>
    
    <div class="form-group">
        <label for="exampleTextarea">Example textarea</label>
        <textarea class="form-control" id="exampleTextarea" rows="3"></textarea>
    </div>
    
    <button type="submit" class="btn btn-primary">Submit</button>
    </form>
    

    JS

         <script>
       $(document).ready(function() {
           $(document).on('contactForm', '#reg-form', function(e) {
            var data = $("#reg-form").serialize();
            e.preventDefault();
               $.ajax({
                   type: 'POST',
                   url: '{{url("/getContact")}}',
                   data: data,
                   success: function(data) {
                    alert("success");
                    console.log(data);
    
                   },
                   error: function(data) {
                       alert("error");
                   }
               });
               return false;
           });
       });
       </script>
    

    Route

    Route::post('/getContact', 'Controller@tryIt');
    

    In the tryit method u should return json response

    public function tryit(Request $request){
    
    //logic here
    
    return response()->json("success");
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?