2017-12-03 15 views
0

AMPフォームからLaravel 5.4コントローラへのajaxリクエストを作成するときにこのエラーが発生します。AMP-HTMLの連絡フォームからLaravel 5.4へのXHRリクエストを作成します。 500 ERROR

これは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', '[email protected]')->name('help'); 
    Route::post('/help-post', '[email protected]')->name('help-post'); 
} 

Iの背面から、この500エラーを表示しますサーバー:

Type error: Argument 1 passed to Illuminate\Session\Middleware\StartSession::addCookieToResponse() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\Routing\Redirector given, called in C:\xampp\htdocs**\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php on line 72

私は、AMP-HTMLで必要とされる土器このグローバル真ん中を追加しました:エラー・スタック内の最後の2行で見ることができるように

<?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を不平を言います応答オブジェクトの代わりにリダイレクターを受信します。

これは、フレームワークaddCookieToResponseが呼び出される(名前空間を照らし\セッション\ミドルウェア\ StartSession.php)での一部です - コードで私のノートをお読みください:

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

あなたcsrf_fieldは(ある)を追加するのを忘れ? –

+0

私はちょうどそれを追加し、今動作します:D Thanks heaps! – Seio

+0

あなたのコードは、 "span visible-when-invalid =" valueMissing ""を使用してフロントエンドのテキストエリアを検証しましたか?テキスト入力ではなく、私の入力のみを検証するように見えるので – Jim41Mavs

答えて

2

あなたはcsrf_field()

<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> 
関連する問題