donglongqiao9595 2016-12-31 18:39
浏览 64
已采纳

将json数据发送到Laravel服务器

I have an angular app where I send data (via json) to my laravel server. My server is on a VM(ubuntu):

This is where I send it to the server from the angular app.

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

On my laravel server I have the route:

Route::get('patientlogin','UploadController@login');

And the controller method

 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();

}

Now when I run the app I've get the error:

POST http://ip/patientlogin 405 (Method Not Allowed)

When I change the get in my web.php to post I get this error:

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

and when I try to call the url in the browser:

MethodNotAllowedHttpException in RouteCollection.php line 218:

Someone any idea what the error could be or what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongxiezhi0590 2016-12-31 19:12
    关注

    HTTP GET cannot have a body(it can, technically, but it's not meant to be). So if you're sending a body in the request you should to use post or put.

    You are getting method not allowed because you configured your route to use GET not POST.

    Change

    Route::get
    

    To

    Route::post 
    

    That should solve it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么