dongruoqiong9017 2015-09-15 09:37
浏览 61
已采纳

在Laravel-5中发布ajax请求

I am trying to send an ajax post request in my page, but I'm getting an error in console.

POST http://local.bazaar.com/testimonials/submit 500 (Internal Server Error)

Routes.php

Route::post('/testimonials/submit','TestimonialsController@savetesti');

Ajax.js

$("#submitTestimonial").click(function() {
    $.ajax({
        url: '/testimonials/submit',
        method: 'POST',               
        success: function(data) {
            alert(data);
            die;
        }
    })

    return false;
});

TestimonialsController.php

<?php
namespace App\Http\Controllers;

use Input;
use Request;

class TestimonialsController extends Controller
{
    /**
     * Perform validations on user data
     * Hash Password
     * Create
     * @return Response
     */
    public function savetesti()
    {
        // Getting all post data
        print_r("success");
    }
}    
?>

viewPage->

<div class="testimonialForm">
    <form action="#" method="POST">
        <div class="testimonialFormTitle">Write A Testimonials</div>
        <a class="closeTestimonialBtn"></a>
        <p><input type="text" placeholder="Your Name" id="tname" name="name" class="inputTxt placeholder"></p>
        <p><input type="text" placeholder="Your Email" id="temail" name="email" class="inputTxt placeholder"></p>
        <p><textarea placeholder="Testimonial" cols="50" rows="6" id="tmessage" name="message" class="inputTxtArea placeholder"></textarea></p>
        <a class="greyBtn" id="submitTestimonial" href="#">Submit</a>
    </form>
</div>
  • 写回答

1条回答 默认 最新

  • duanchigeng4313 2015-09-15 11:30
    关注

    I think the problem is the csrf token.
    When you do a post request there should always be a csrf token.

    If you have created a form with illuminate/html facade then its quite simple

    {!! Form::open() !!}
    {!! Form::close() !!}
    

    If you do not have illuminate/html go to your composer.json file and add

    "illuminate/html": "~5.0", 
    

    In the require block after laravel. After that run composer update and go to /config/app.php here you should add a new provider and alias

    'providers' => [
        'Illuminate\Html\HtmlServiceProvider',
    ],
    
    'aliases' => [
        'Form' => 'Illuminate\Html\FormFacade',
    ],
    

    Now inside this form there is a hidden input field with the crsf token.
    In you javascript file you can get this code with something like.

    var data = {};
    data._token = $('[name="_token"]').val();
    

    Then send the data object with the ajax post call

    $.ajax({
        url: '/testimonials/submit',
        method: 'POST',   
        data: data,            
        success: function(data){
            alert(data);
        }
    });
    

    And for test purpose you could change your function

    public function savetesti()
    { 
        return 'success';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答