2016-12-31 15 views
0

私はラーバルサーバーにデータを(json経由で)送信するアングルアプリを持っています。 私のサーバはVM(ubuntu)上にあります:jsonデータをLaravelサーバーに送信

これは私が角型アプリケーションからサーバに送信する場所です。

:私はアプリを実行すると

public function login(Request $request){ 
    // error_reporting(-1); // prints every error, warning, etc 
    error_reporting(0); // no output at all 

    // set content-type of response to json 
    header('Content-Type: application/json'); 

    // import Auth class and custom functions 
    // require_once('custom_functions.php'); 


    $LOGIN_LOG_FILE = "login1.log"; 
    $AUTH_HEADERS_FILE = "auth-headers1.txt"; 


    /* 
     php://input is raw input, regardless of header field "content-type" 
     The PHP superglobal $_POST, only is supposed to wrap data that is either 
     application/x-www-form-urlencoded or multipart/form-data-encoded 
     http://stackoverflow.com/a/8893792 

     When sending only a JSON, $_POST etc will not be populated and php://input has to be used 
     in the php scripts 
     http://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission 
     http://php.net/manual/de/wrappers.php.php 
    */ 



    $content = $request->instance(); 
    $json_raw = $content->json()->all(); 
    $json = json_decode($json_raw, true); 


    /* <-- DEBUGGING START TODO delete */ 
    //read the header, where username and password are supposed to be in 
    $headers = apache_request_headers(); 

    //print the contents of the headers array in a neat structure and log them 
    $headersPrintable = print_r($headers, true); 
    file_put_contents($AUTH_HEADERS_FILE, $headersPrintable, FILE_APPEND); 

    $request = print_r($_REQUEST, true); 
    $post = print_r($_POST, true); 
    file_put_contents("auth-req.txt", $request, FILE_APPEND); 
    file_put_contents("auth-post.txt", $post, FILE_APPEND); 

    file_put_contents("auth-req-json.txt", $json_raw, FILE_APPEND); 
    file_put_contents("auth-req-json_decoded.txt", $json, FILE_APPEND); 
    /* DEBUGGING END --> */ 

    $valid = false; 
    $username = ""; 

    //check if username and passord exist in the json-decoded version of php://input 
    if(array_key_exists("username", $json) && array_key_exists("password", $json)) { 
     $username = $json["username"]; 
     $password = $json["password"]; 
     $valid = Auth::checkCredentials($username, $password); 
    } 

    $response = array(
     "username" => $username, 
     "valid" => $valid 
    ); 

    echo json_encode($response); 

    //exit(); 

} 

は今、私はエラーを取得しました

Route::get('patientlogin','[email protected]'); 

とコントローラ方法:私のlaravelサーバで

this.http.post(this.loginURL, requestBody, options) 

私はルートを持っています

POST http://ip/patientlogin 405(方法が許可されていません)

私が投稿する私のweb.phpで取得変更するとき、私はこのエラーを取得:

polyfills.js:1 POST http://ip/patientlogin 500 (Internal Server Error) 

と私はブラウザにURLを呼び出そう:

MethodNotAllowedHttpException in RouteCollection.php line 218: 

誰かが任意のアイデアエラーは何か、何が間違っているのでしょうか?

+0

それを解決する必要があり

Route::post 

変更

Route::get 

。 'Route :: get'とあなたは投稿しようとしています。 – Erevald

+0

エラーの詳細を確認するには、エラーログを確認するか、デベロッパーツールのレスポンス結果を確認する必要があります。ブラウザでポストルートを呼び出すと、ブラウザがget ..経由でリクエストしているのでエラーが表示されるので、ポストはうまくいくか、または両方を定義する必要があります。 –

+0

this.http.postをthis.http.getなどに変更します(使用可能な場合)。または、ポストルータがcsrf verify middleware –

答えて

1

HTTP GETには、本文を含めることはできません(技術的には可能ですが、そうでないこともあります)。したがって、あなたがリクエストで本文を送信している場合は、投稿を使用するか、putを使用する必要があります。

GETではなくPOSTを使用するように経路を設定したため、方法が許可されていません。あなたのルートがGETであるためだそれ

+0

リクエストを取得するにはボディがありませんか? Lol –

+0

Emmmmは間違っていますか?何で(笑? –

+0

GETはhttpメソッドの単なる動詞です。すべての方法は体を持つことができます。 –

関連する問題