2017-06-20 6 views
0

こんにちは私は角度jでポストリクエストを使ってフォームデータを送信しようとしています。ここでのコードは次のとおり複数のパラメータを角度jsのポストリクエストとしてラベールバックエンドに送信

contact.html(laravelで)

angular.module('app.controllers',[ 
     'app.directives', 
     'app.factories' 
     ]).controller('ContactController',['$scope','$http','$httpParamSerializer',function($scope,$http,$httpParamSerializer){ 
      $scope.submit = function() { 
var text = {"first_name":$scope.fname,"last_name":$scope.lname,"email":$scope.email,"reason":$scope.reason,"description":$scope.description}; 
       $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 
       $http.post('/api/contact',$httpParamSerializer(text)).then(function (data, status, headers, config) { 
        alert("success"); 
        console.log(data); 
        console.log(status); 
        console.log(headers); 
        console.log(config); 
       },function (data, status, headers, config) { 
        alert("error"); 
        console.log(data); 
        console.log(status); 
        console.log(headers); 
        console.log(config); 
       }); 
      } 
     }]); 

web.php

<form class="acodehub-form" ng-submit="submit()" method="post" novalidate> 
     <div class="form-group first_name"> 
      <label for="first_name">First Name</label><br> 
      <input type="text" id="first_name" ng-model="fname" name="fname" class="acodehub-input form-control " required tabindex="10" maxlength="40" value="" placeholder="First Name" /> 
      <div class="errorMessage" ng-show="errorFN">First name cannot be empty</div> 
     </div> 
     <div class="form-group last_name"> 
      <label for="last_name">Last Name</label><br> 
      <input type="text" id="last_name" ng-model="lname" name="lname" class="acodehub-input form-control " required tabindex="11" maxlength="40" value="" placeholder="Last Name" /> 
      <div class="errorMessage" ng-show="errorLN">Last name cannot be empty</div> 
     </div> 
     <div class="form-group inputEmail"> 
      <label for="email">Email</label><br> 
      <input type="email" id="inputEmail" ng-pattern = "regexemail" ng-model="email" name="email" class="acodehub-input form-control" required tabindex="12" value="" maxlength="40" placeholder="Email" /> 
      <div class="errorMessage" ng-show="errorE">Please enter a valid email address</div> 
     </div> 
     <div class="form-group reason"> 
      <label for="reason">Subject</label> 
      <select name="reason" ng-model="reason" id="reason" class="typeselects acodehub-input acodehub-select form-control" placeholder="What can we help you with?" tabIndex="13" required> 

       <option selected value="">What can we help you with?</option> 
       <option value="Marketing">Marketing</option> 
       <option value="Advertise" >Advertise</option> 
       <option value="Product Review">Product Review</option> 
       <option value="Tutorial Request">Tutorial Request</option> 
       <option value="Freebie Request">Freebie Request</option> 
       <option value="Write for us" >Write for us</option> 
       <option value="Sticker Request">Ask a question?</option> 
       <option value="Privacy Policy" >Privacy Policy</option> 
       <option value="Other">Other</option> 
      </select> 
      <div class="errorMessage" ng-show="errorR">Select a valid subject</div> 
     </div> 
     <div class="form-group inputDescription"> 
      <label for="inputDescription">Tell Us More</label><br> 
      <textarea name="description" ng-model="description" value="" id="inputDescription" required class="form-control acodehub-input acodehub-textarea" tabindex="14"></textarea> 
      <div class="errorMessage" ng-show="errorD">Please tell us something about your query. Minimum 50 letters.</div> 
     </div> 
     <div> 
      <button type="submit" class="acodehub-btn acodehub-btn-dark" 
      data-ga-track="true" data-ga-group="Contact Us" 
      data-ga-event="Contact Us - Click to Submit Form" 
      data-ga-val="Submit"> 
      Submit 
     </button> 
    </div> 
</form> 

controller.js

Route::post('/api/contact','[email protected]'); 

contactcontroller

public function submit(Request $request) { 
     $this->validate($request,array(
      'first_name'=>'required', 
      'last_name'=>'required',    
      'email'=>'required|email', 
      'reason'=>'required', 
      'description'=>'required|min:20' 
     )); 

     $data = array(
      'first_name'=>$request->first_name, 
      'last_name'=>$request->last_name,   
      'email'=>$request->email, 
      'reason'=>$request->reason, 
      'description'=>$request->description 
     ); 
     Mail::queue('emails.contact',$data,function($message) use($data) { 
      $message->from($data['email']); 
      $message->to('[email protected]'); 
      $message->subject($data['reason']); 
     }); 


     //dd($request); 

     //echo 'hello '.$submit; 
     if(count(Mail::failures())>0) { 
      return $request; 
     }else { 
      return $request; 
     } 
     //return $data; 
    } 

ように私は、コンソールに出力を取得しています:私はstackoverflowのか、他のウェブサイトで提供すべてのソリューションを試してみましたが、私はそれを正しく設定することはできませんよ、毎回私は同じ出力を取得しています

Object {data: "<!DOCTYPE html> 
↵<html ng-app="app"> 
↵<head> 
↵ <b…rc="/app/services.js"></script> 
↵</body> 
↵</html>", status: 200, config: Object, statusText: "OK", headers: function} 
undefined 
undefined 
undefined 

上記。私はどこかで何かが不足していることを知っていますが、今は正しく設定する方法がありません。私がそれを解決するのを助けてください。

答えて

0

$ httpは約束を返します。成功またはエラーが発生した場合は、data、status、config、statusText、およびheadersプロパティ(コンソールログに表示されているように)を含むオブジェクトが解決または拒否されます。だから、

のために、コードを変更します。私はあなたが上記の私を示唆したものを実装しますが、私のようにresponse.dataを取得しています

$http.post('/api/contact',$httpParamSerializer(text)).then(function (response) { 
     alert("success"); 
     console.log(response.data); 
     console.log(response.status); 
     console.log(response.headers); 
     console.log(response.config); 
    },function (error) { 
     alert("error"); 
     console.log(error.data); 
     console.log(error.status); 
     console.log(error.headers); 
     console.log(config); 
    }); 
+0

: –

+0

推奨されていません
:自動的に$ HTTP_RAW_POST_DATAを投入することは推奨されていませんとなります将来のバージョンで削除されました。この警告を避けるには、php.iniの 'always_populate_raw_post_data'を '-1'に設定し、代わりにphp://入力ストリームを使用します。 <!DOCTYPE HTML>すでにラインに不明で送信されたヘッダを
<メタのcharset = - ヘッダー情報を変更することはできません:ライン上の不明

+0

警告で"UTF-8" /> http:// locaにリダイレクトするlhost:8000/contact http://localhost:8000/contactにリダイレクトします。 –