首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery ajax不工作(Laravel 4)

Jquery ajax不工作(Laravel 4)
EN

Stack Overflow用户
提问于 2014-03-11 20:12:02
回答 2查看 473关注 0票数 1

我使用jquery ajax将数据添加到数据库中。当我单击submit按钮时,我的页面返回空白。我使用firebug进行调试,并看到消息: 500 (内部服务器错误)。

routes.php

代码语言:javascript
复制
Route::controller('subscribers', 'SubscribersController');

SubscribersController.php

代码语言:javascript
复制
class SubscribersController extends \BaseController {

//The method to show the form to add a new feed
public function getIndex() {
    //We load a view directly and return it to be served
    return View::make('subscribe_form');
}

//This method is to process the form
public function postSubmit() {

    //We check if it's really an AJAX request
    if(Request::ajax()) {
        $validation = Validator::make(Input::all(), array(
        //email field should be required, should be in an email
        //format, and should be unique
        'email' => 'required|email|unique:subscribers, email'));

        if($validation->fails()) {
            return $validation->errors()->first();
        } else {
            $create = Subscribers::create(array(
                'email' => Input::get('email')
            ));
            //If successful, we will be returning the '1' so the form
            //understands it's successful or if we encountered an unsuccessful creation attempt, 
            //return its info
            return $create?'1':'We could not save your address to our system, please try again later';
        }
    } else {
        return Redirect::to('subscribers');
    }
}

}

查看文件:

代码语言:javascript
复制
{{--Form Starts Here --}}
{{Form::open(array('url' => URL::to('subscribers/submit'), 'method' => 'post'))}}
<p>Simple Newsletter Subscription</p>
{{Form::text('email', null, array('placeholder'=>'Type your E-mail address here'))}}
{{Form::submit('Submit!')}}

{{Form::close()}}
{{--Form Ends Here --}}

{{--This div will show the ajax response --}}
<div class="content"></div>
{{-- Because it'll be sent over Ajax, we add the jQuery source --}}
{{HTML::script('http://code.jquery.com/jquery-1.11.0.min.js') }}
<script type="text/javascript">
//Even though it's on footer, I just like to make
//sure that DOM is ready
$(function() {
    //We hide de the result div on start
    $('div.content').hide();
    //This part is more jQuery related. In short, we make an Ajax post request and get
    //the response back from server
    $('input[type="submit"]').click(function(e) {

        e.preventDefault();

        $.post('http://localhost/laravel-blueprint/newsletter/public/subscribers/submit', {email: $('input[name="email"]').val()
            }, function($data) {
                if($data == '1') {
                    $('div.content')
                        .hide()
                        .removeClass('success error')
                        .addClass('success')
                        .html('You\'ve successfully subscribed to our newsletter')
                        .fadeIn('fast');

                } else {
                    //This part echos our form validation errors
                    $('div.content')
                        .hide().removeClass('success error')
                        .addClass('error')
                        .html('There has been an error occurred: <br /><br />'+$data)
                        .fadeIn('fast');
                }
            });
    });
    //We prevented to submit by pressing enter or any other way
    $('form').submit(function(e) {
        e.preventDefault();
        $('input[type="submit"]').click();
    });
    });
</script>

我使用的是laravel 4 log-access:

代码语言:javascript
复制
127.0.0.1 - - [11/Mar/2014:17:54:41 +0700] "POST /laravel-blueprint/newsletter/public/subscribers/submit HTTP/1.1" 500 381

有什么解决方案吗?

EN

回答 2

Stack Overflow用户

发布于 2014-03-12 16:55:54

要使您的代码正常工作,请执行以下更改:

订户控制器

代码语言:javascript
复制
class SubscribersController extends \BaseController {

    public function getIndex() {

        return View::make('subscribe_form');
    }

    public function postSubmit() {

        if(Request::ajax()) {

            $validation = Validator::make(Input::all(), 

            ['email' => 'required|email|unique:subscribers,email']);

            if($validation->fails()) {

                return  $validation->errors()->first();

            } else {

                // Note here that the model is Subscriber and not Subscribers

                // This is the default convention for the subscribers table

                $create = Subscriber::create(array(

                  'email' => Input::get('email')

                ));

                return $create ? '1' : 'We could not save your address';
            }
        } else {

            return Redirect::to('subscribers');
        }
    }
}

对于订户模型来说,很重要

代码语言:javascript
复制
class Subscriber extends Eloquent {

    // I don't know if you have timestamps enabled, but if not this is necessary

    public $timestamps = false; 

    // Must be present for mass assignment to work (Subscriber::create)

    protected $fillable = array('email');
}
票数 1
EN

Stack Overflow用户

发布于 2014-03-11 22:47:35

评论

500 (Internal Server Error)可能是由PHP致命引起的。

你开着error_reporting吗?如果没有,请尝试

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

检查一下。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22325380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档