【问题标题】:Sending json data to Laravel server将 json 数据发送到 Laravel 服务器
【发布时间】:2016-12-31 18:39:23
【问题描述】:

我有一个角度应用程序,我将数据(通过 json)发送到我的 laravel 服务器。 我的服务器在 VM(ubuntu) 上:

这是我从 Angular 应用程序将其发送到服务器的地方。

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

在我的 laravel 服务器上,我有路由:

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

还有控制器方法

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

}

现在当我运行应用程序时出现错误:

POST http://ip/patientlogin 405(方法不允许)

当我将 web.php 中的 get 更改为发布时,我收到此错误:

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

当我尝试在浏览器中调用网址时:

MethodNotAllowedHttpException in RouteCollection.php line 218:

有人知道错误可能是什么或我做错了什么吗?

【问题讨论】:

  • 那是因为你的路线是一个get。 Route::get 你正在尝试发帖。
  • 我认为您需要检查错误日志或查看开发人员工具中的响应结果以获取有关错误的更多信息。如果您在浏览器中调用 post 路由,则会收到错误,因为浏览器正在通过 get 请求.. 所以 post 应该没问题或同时定义两者。
  • 将 this.http.post 更改为 this.http.get 或其他内容(如果可用)或更改您的服务器路由类型,因为 post 路由器将其从 csrf 验证中间件中排除
  • 我怎样才能排除它?以前没做过,

标签: php angularjs laravel


【解决方案1】:

HTTP GET 不能有正文(从技术上讲,它可以,但它不是故意的)。因此,如果您在请求中发送正文,您应该使用 post 或 put。

您正在获取不允许的方法,因为您将路线配置为使用 GET 而不是 POST。

改变

Route::get

Route::post 

应该可以解决

【讨论】:

  • 获取请求没有正文?大声笑
  • GET 只是 http 方法的一个动词。每个方法都可以有一个主体。
  • 我可以,但这在语义上是不正确的......我会更新我的答案
猜你喜欢
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 2013-12-27
  • 1970-01-01
相关资源
最近更新 更多