2017-06-22 55 views
0

我一直試圖從角度發佈數據到php整天,但我感覺相當卡住。 這是角度控制我的方法:角度post方法到php

function submit() {  
     var JSONObject = { 
      "name":$rootScope.name, 
      "surname":$rootScope.surname, 
      "email":$rootScope.email, 
      "review":$rootScope.review 
     }; 
     debugger; 
     var Results = UniversalService.PostReview(JSONObject); 
    } 

然後它去UniversalService在我的$ http.post方法是:

function PostReview(JSONObject) { 
     debugger; 
     $http.post('http://localhost:8000/creation', JSONObject).then(); 
     debugger; 
    } 

一切順利,直到當它發送數據到這一點PHP(我的網頁API服務是流明框架)

在我DatabaseController.php我希望收到我的JSON對象

(我很初學者,所以我希望這就是我失敗)

public function created(Request $request) 
{ 
    $inputJSON = file_get_contents('php://input'); 
    $input= json_decode($inputJSON, TRUE); //convert JSON into array 

    print_r(json_encode($input)); 
    /* $user=[ 
     'name'=>$request->input('name'), 
     'surname'=>$request->input('surname'), 
     'email'=>$request->input('email'), 
     'review'=>$request->input('review'), 
    ]; 
    $time=$this->getTime();*/ 
// return DatabaseModel::newUser($user); 

} 

在routes.php文件

$app->post('/creation', [ 'as'=>'creation','uses'=> '[email protected]']); 

我用GET和POST這裏走出的絕望,我似乎無法得到它的權利都沒有。也在routes.php我啓用標題('訪問控制,允許來源:*');而且我不確定我是否需要在角度上做到這一點(以及如何)。

什麼,我得到的是一堆錯誤enter image description here

但郵遞員似乎工作,我有附加功能還有,當我進入:http://localhost:8000/creation到郵遞員,我得到空從我在調用另一個函數的JSON對象和時間創建()

+1

你'後'到'獲取'URL – aynber

+0

我改變'get'到'後'routes.php,仍然有問題:( – user122222

+0

顯示你的錯誤js和路線都有帖子。 – aynber

回答

1

解決我的問題今天與其他三個編程人員。正如@jheimbouch所說,問題在於Angular向PHP服務發送OPTIONS方法。所陳述的另一個問題是cors。我們的解決方案是:在 我們/bootstrap/app.php補充​​說: $app->middleware(['Cors' => App\Http\Middleware\CorsMiddleware::class]);

然後在/ HTTP /中間件添加文件CorsMiddleware.php這解決了我的所有問題:

<?php 

命名空間應用\ HTTP \中間件;

使用關閉; 使用Illuminate \ Http \ Response;

類CorsMiddleware {

protected $settings = [ 
      'origin' => '*', // Wide Open! 
      'allowMethods' => 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS']; 

protected function setOrigin($req, $rsp) 
{ 
    $origin = $this->settings['origin']; 
    if (is_callable($origin)) 
    { 
     // Call origin callback with request origin 
     $origin = call_user_func($origin, $req->header("Origin")); 
    } 
    $rsp->header('Access-Control-Allow-Origin', $origin); 
} 

protected function setExposeHeaders($req, $rsp) 
{ 
    if (isset($this->settings->exposeHeaders)) 
    { 
     $exposeHeaders = $this->settings->exposeHeaders; 

     if (is_array($exposeHeaders)) 
     $exposeHeaders = implode(", ", $exposeHeaders); 

     $rsp->header('Access-Control-Expose-Headers', $exposeHeaders); 
    } 
} 

protected function setMaxAge($req, $rsp) 
{ 
    if (isset($this->settings['maxAge'])) 
    $rsp->header('Access-Control-Max-Age', $this->settings['maxAge']); 
} 

protected function setAllowCredentials($req, $rsp) 
{ 
    if (isset($this->settings['allowCredentials']) && $this->settings['allowCredentials'] === True) 
    $rsp->header('Access-Control-Allow-Credentials', 'true'); 
} 

protected function setAllowMethods($req, $rsp) 
{ 
    if (isset($this->settings['allowMethods'])) 
    { 
     $allowMethods = $this->settings['allowMethods']; 

     if (is_array($allowMethods)) 
     $allowMethods = implode(", ", $allowMethods); 

     $rsp->header('Access-Control-Allow-Methods', $allowMethods); 
    } 
} 

protected function setAllowHeaders($req, $rsp) 
{ 
    if (isset($this->settings['allowHeaders'])) 
    { 
     $allowHeaders = $this->settings['allowHeaders']; 

     if (is_array($allowHeaders)) 
     $allowHeaders = implode(", ", $allowHeaders); 

    } 
    else // Otherwise, use request headers 
    { 
     $allowHeaders = $req->header("Access-Control-Request-Headers"); 
    } 

    if (isset($allowHeaders)) 
    $rsp->header('Access-Control-Allow-Headers', $allowHeaders); 

} 

protected function setCorsHeaders($req, $rsp) 
{ 
    // http://www.html5rocks.com/static/images/cors_server_flowchart.png 
    // Pre-flight 
    if ($req->isMethod('OPTIONS')) 
    { 
     $this->setOrigin($req, $rsp); 
     $this->setMaxAge($req, $rsp); 
     $this->setAllowCredentials($req, $rsp); 
     $this->setAllowMethods($req, $rsp); 
     $this->setAllowHeaders($req, $rsp); 
    } 
    else 
    { 
     $this->setOrigin($req, $rsp); 
     $this->setExposeHeaders($req, $rsp); 
     $this->setAllowCredentials($req, $rsp); 
    } 
} 

/** 
* Handle an incoming request. 
* 
* @param \Illuminate\Http\Request $request 
* @param \Closure $next 
* @return mixed 
*/ 
public function handle($request, Closure $next) { 

    if ($request->isMethod('OPTIONS')) { 
     $response = new Response("", 200); 
    } 
    else { 
     $response = $next($request); 
    } 

    $this->setCorsHeaders($request, $response); 

    return $response; 
}} 

希望這會幫助別人,因爲我花了年齡把它全部完成!無論如何,非常感謝大家誰幫助:)

+0

很高興你有它工作! – jheimbouch

1

我相信你會受到OPTIONS預鍍鉻前的要求。在控制檯中注意它抱怨你的「OPTIONS」請求,而不是POST本身。在沒有看到您的服務器代碼的情況下,下面是一個如何處理它的例子,以及需要在您的OPTIONS pre-flight的響應中使用的標題。

// respond to preflights 
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { 
    // return only the headers and not the content 
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']){ 
    header('Access-Control-Allow-Origin: *'); 
    header('Access-Control-Allow-Headers: X-Requested-With'); 
    } 
    exit; 
} 

更詳盡的解釋,(凡我抓起段)可以在這裏找到:https://remysharp.com/2011/04/21/getting-cors-working