从AMP-HTML联系表单向Laravel 5.4发出XHR请求。 500错误

时间:2017-12-03 13:52:40

标签: laravel symfony xmlhttprequest laravel-5.4 amp-html

从AMP表单向Laravel 5.4控制器发出ajax请求时出现此错误。

这是AMP-HTML表单:

<form method="post"
    action-xhr="/mobile/help-post"
    target="_top"
    on="submit:msg.hide;submit-success:success-lightbox;submit-error:error-lightbox">

    <fieldset>
        <span>Your name</span>
        <input placeholder="Write here.." type="text" name="name" id="name" required>
    </fieldset>

    <fieldset>  
        <span>Your email</span>
        <input placeholder="Write here.." type="email" name="email" id="email" required>
    </fieldset>

    <fieldset>  
        <span>Your message</span>
        <textarea placeholder="Write here" name="message" id="email" required></textarea>
    </fieldset>

    <fieldset class="btn">  
        <input class="btn-primary" type="submit" value="Send Help Request »">
    </fieldset>

    <!--Error Messages-->
    <span visible-when-invalid="valueMissing" validation-for="name"></span>
    <span visible-when-invalid="valueMissing" validation-for="email"></span>
    <span visible-when-invalid="valueMissing" validation-for="message"></span>

    <span visible-when-invalid="typeMismatch" validation-for="name"></span>
    <span visible-when-invalid="typeMismatch" validation-for="email"></span>
    <span visible-when-invalid="typeMismatch" validation-for="message"></span>
    <!--End Error Messages-->

    <div submit-success>
        <template type="amp-mustache">
            Success! Sit tight and we'll get back to you soon!
        </template>
    </div>
    <div submit-error>
        <template type="amp-mustache">
            Oops!, We apologies something went wrong. Please try again later.
        </template>
    </div>
</form>

这是控制器:

public function postHelp(Request $request) {
    $this->validate($request, [
        'email' => 'required|email',
        'name' => 'required|min:3',
        'message' => 'required|min:10']);

    $data = array(
        'email' => $request->email,
        'name' => $request->subject,
        'bodyMessage' => $request->message
    );

    Mail::to('***@gmail.com')
    ->send($data);

    $response = array(
        'status' => 'success',
        'msg' => 'Setting created successfully',
    );
    return response($response, 200);
}

这是路线:

Route::prefix('mobile')->group(function(){
    Route::get('/help', 'PagesController@getHelp')->name('help');
    Route::post('/help-post', 'PagesController@postHelp')->name('help-post');
}

我从服务器收到此500错误:

  

类型错误:参数1传递给   Illuminate \ Session \ Middleware \ StartSession :: addCookieToResponse()必须   是一个Symfony \ Component \ HttpFoundation \ Response实例的实例   给出的Illuminate \ Routing \ Redirector,调用   C:\ XAMPP \ htdocs中** \供应商\ laravel \框架的\ src \照亮\会议\中间件\ StartSession.php   在第72行

我已根据AMP-HTML的要求添加了此全局中间件:

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class AddHeaders
{
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);
        $response->header("Access-Control-Allow-Origin", '*'); 
        $response->header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        $response->header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
        $response->header("Access-Control-Expose-Headers", "AMP-Access-Control-Allow-Source-Origin");
        $response->header("AMP-Access-Control-Allow-Source-Origin", 'http://localhost:8000');
        $response->header("Access-Control-Allow-Credentials", "true");

        return $response;
    }
}

正如您在错误堆栈的最后两行中所看到的那样:

in StartSession.php (line 170)
at StartSession->addCookieToResponse(object(Redirector), object(Store))

它抱怨addCookieToResponse收到重定向器而不是Response对象。

这是框架中的部分(命名空间Illuminate \ Session \ Middleware \ StartSession.php),其中调用了addCookieToResponse - 请在代码中阅读我的注释:

public function handle($request, Closure $next)
    {
        $this->sessionHandled = true;

        // If a session driver has been configured, we will need to start the session here
        // so that the data is ready for an application. Note that the Laravel sessions
        // do not make use of PHP "native" sessions in any way since they are crappy.
        if ($this->sessionConfigured()) {
            $request->setLaravelSession(
                $session = $this->startSession($request)
            );

            $this->collectGarbage($session);
        }

        $response = $next($request);

        // Again, if the session has been configured we will need to close out the session
        // so that the attributes may be persisted to some storage medium. We will also
        // add the session identifier cookie to the application response headers now.
        if ($this->sessionConfigured()) {
            $this->storeCurrentUrl($request, $session);

            $this->addCookieToResponse($response, $session);-->LOOK HERE: so is $response here a Redirector object ??????? But it's $response = $next($request); !!!!!!
        }

        return $response;
    }

我非常怀疑框架中有什么东西。但是,我无法告诉我返回重定向器的位置而不是响应。从表单到控制器的路径返回表单的过程简单明了!

1 个答案:

答案 0 :(得分:2)

您忘了添加<form method="post" action-xhr="/mobile/help-post" target="_top" on="submit:msg.hide;submit-success:success-lightbox;submit-error:error-lightbox"> {{csrf_field()}} <fieldset> <span>Your name</span> <input placeholder="Write here.." type="text" name="name" id="name" required> </fieldset> <fieldset> <span>Your email</span> <input placeholder="Write here.." type="email" name="email" id="email" required> </fieldset> <fieldset> <span>Your message</span> <textarea placeholder="Write here" name="message" id="email" required></textarea> </fieldset> <fieldset class="btn"> <input class="btn-primary" type="submit" value="Send Help Request »"> </fieldset> <!--Error Messages--> <span visible-when-invalid="valueMissing" validation-for="name"></span> <span visible-when-invalid="valueMissing" validation-for="email"></span> <span visible-when-invalid="valueMissing" validation-for="message"></span> <span visible-when-invalid="typeMismatch" validation-for="name"></span> <span visible-when-invalid="typeMismatch" validation-for="email"></span> <span visible-when-invalid="typeMismatch" validation-for="message"></span> <!--End Error Messages--> <div submit-success> <template type="amp-mustache"> Success! Sit tight and we'll get back to you soon! </template> </div> <div submit-error> <template type="amp-mustache"> Oops!, We apologies something went wrong. Please try again later. </template> </div>

# calculate the real root(s) of a quadratic polynomial
# coefficients of which are given as aa, bb and cc
quadratic_roots <- function(aa = 1, bb = 2, cc = 1)
{
    # calculate the discriminant
    disc <- bb^2 - 4 * aa * cc

    if (disc == 0) # if1, single root
    {
        return(-bb / (2 * aa))
    }   
    else
    {
        if (disc > 0) # if2, two roots
        {
            root1 <- (-bb - sqrt(disc)) / (2 * aa)
            root2 <- (-bb + sqrt(disc)) / (2 * aa)
            return(c(root1, root2))

        }
        else # no real roots, return NA
        {
            return(NA)
        }   
    }   
}