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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?