将JSON数据发送到Laravel服务器

缩略语44

我有一个角度应用程序,在这里我通过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:

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

阿曼纽尔·内加(Amanuel Nega)

HTTP GET不能具有主体(从技术上讲,它可以,但并非必须如此)。因此,如果要在请求中发送正文,则应使用post或put。

您获取的方法不允许,因为您将路由配置为使用GET not POST。

改变

Route::get

Route::post 

那应该解决

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过JSON将数据发送到服务器

来自分类Dev

无法将数据发送到libwebsocket服务器

来自分类Dev

将数据发送到Delphi服务器

来自分类Dev

将大型画布数据发送到服务器

来自分类Dev

将数据从服务器发送到Android设备

来自分类Dev

Android将数据发送到服务器问题

来自分类Dev

无法将数据发送到libwebsocket服务器

来自分类Dev

将大型画布数据发送到服务器

来自分类Dev

Android JSON解析器将数据的多个副本发送到服务器

来自分类Dev

将JSON发送到服务器

来自分类Dev

Android将JSON发送到服务器

来自分类Dev

如何使用Javascript XMLHttpRequest将JSON数据对象发送到服务器?

来自分类Dev

通过JavaScript将JSON数据发送到服务器

来自分类Dev

使用Ajax POST请求将图像和JSON数据发送到服务器?

来自分类Dev

使用AT命令将Json数据发送到服务器

来自分类Dev

如何使用JSON将这些<div>中的数据发送到服务器?

来自分类Dev

我试图将这个json数据形式的android发送到php服务器

来自分类Dev

将JSON数据和图像发送到服务器

来自分类Dev

我可以将大型JSON数据从android发送到php服务器吗?

来自分类Dev

如何将多个json对象的数据发送到android中的服务器?

来自分类Dev

需要通过JavaScript以JSON格式将数据发送到服务器

来自分类Dev

如何将多个json对象的数据发送到android中的服务器?

来自分类Dev

通过AJAX将表单JSON数据发送到服务器

来自分类Dev

如何通过点击按钮从uitableview将选定的JSON数据值发送到服务器

来自分类Dev

将 JSON 数据发送到服务器并通过 AJAX 返回 - 涉及的步骤

来自分类Dev

angularjs 在使用 POST 将数据发送到服务器时使 json 格式错误

来自分类Dev

定期使用python将Json数据从文件发送到服务器

来自分类Dev

如何将json数据发送到服务器并在android中获得Json响应?

来自分类Dev

将GPS坐标发送到服务器的服务

Related 相关文章

  1. 1

    通过JSON将数据发送到服务器

  2. 2

    无法将数据发送到libwebsocket服务器

  3. 3

    将数据发送到Delphi服务器

  4. 4

    将大型画布数据发送到服务器

  5. 5

    将数据从服务器发送到Android设备

  6. 6

    Android将数据发送到服务器问题

  7. 7

    无法将数据发送到libwebsocket服务器

  8. 8

    将大型画布数据发送到服务器

  9. 9

    Android JSON解析器将数据的多个副本发送到服务器

  10. 10

    将JSON发送到服务器

  11. 11

    Android将JSON发送到服务器

  12. 12

    如何使用Javascript XMLHttpRequest将JSON数据对象发送到服务器?

  13. 13

    通过JavaScript将JSON数据发送到服务器

  14. 14

    使用Ajax POST请求将图像和JSON数据发送到服务器?

  15. 15

    使用AT命令将Json数据发送到服务器

  16. 16

    如何使用JSON将这些<div>中的数据发送到服务器?

  17. 17

    我试图将这个json数据形式的android发送到php服务器

  18. 18

    将JSON数据和图像发送到服务器

  19. 19

    我可以将大型JSON数据从android发送到php服务器吗?

  20. 20

    如何将多个json对象的数据发送到android中的服务器?

  21. 21

    需要通过JavaScript以JSON格式将数据发送到服务器

  22. 22

    如何将多个json对象的数据发送到android中的服务器?

  23. 23

    通过AJAX将表单JSON数据发送到服务器

  24. 24

    如何通过点击按钮从uitableview将选定的JSON数据值发送到服务器

  25. 25

    将 JSON 数据发送到服务器并通过 AJAX 返回 - 涉及的步骤

  26. 26

    angularjs 在使用 POST 将数据发送到服务器时使 json 格式错误

  27. 27

    定期使用python将Json数据从文件发送到服务器

  28. 28

    如何将json数据发送到服务器并在android中获得Json响应?

  29. 29

    将GPS坐标发送到服务器的服务

热门标签

归档