简体   繁体   English

出现 500 错误,因为 ajax 无法访问我的 php

[英]Getting 500 error with because ajax can't access my php

I am trying to develop the front end of a simple login test webpage.我正在尝试开发一个简单的登录测试网页的前端。 Basically I the website should be able to just display to the user if the login was or was not successful.基本上,如果登录成功或不成功,我的网站应该能够只向用户显示。 The message should be text right below the login form.该消息应该是登录表单正下方的文本。 The page should not have to be restarted, hence the ajax.页面不应该重新启动,因此 ajax. I have to make the markup for the page, use ajax to send the data to my PHP file, where I curl my information to the middle end.我必须为页面做标记,使用 ajax 将数据发送到我的 PHP 文件,在那里我将我的信息卷曲到中间。 The middle end will then send me a response and I am to parse that into a string and just display the string to the user.然后中间端会向我发送一个响应,我将把它解析成一个字符串,然后将该字符串显示给用户。 The middle end will return something like: {"message":"login failed","success":false} .中间端将返回类似: {"message":"login failed","success":false} And I would just need to put a message right below the log in form saying login failed .我只需要在登录表单正下方放置一条消息,说login failed My three files are included below:我的三个文件包括在下面:

index.html索引.html

<!DOCTYPE html>
<html>
  <head>

    <title>Login Alpha</title>

  </head>

  <body>
    <h1>Log In</h1>
    <br>
    <form name="alphalogin" method="post" action="front-connect.php" id="input_form">
      <input type="text" name="user" id="user" placeholder="User..."><br>
      <input type="password" name="password" id="password" placeholder="Password..."><br>
      <input type="button" value="Log In" id="login">
    </form>
    <div id="responce"></div>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="scripts.js"></script>
  </body>
</html>

scripts.js脚本.js

$(document).ready(function () {
    $("#login").on('click', function () {
    var user = $("#user").val();
    var password = $("#password").val();

    if (user == "" || password == "")
        alert('Please check inputs');
    else {
    $.ajax(
        {
        url: 'front-connect.php',
        method: 'POST',
        data: {
            user: user,
            password: password
        },

        success: function (responce) {
            $("#responce").html(responce);
        },
        dataType: 'text'
        });
    }
    });
});

front-connect.php前端连接.php

<?php
$parmas = json_decode(file_get_contents('php://input'));

$user = $parmas->{'user'};
$password = $parmas->{'password'};

$post = array("user" => $user, "pass" => $password);
$transfer = json_encode($post);

$ctrl = curl_init();
curl_setopt($ctrl, CURLOPT_URL, "https://web.njit.edu/~bgb7/login/login.php");
curl_setopt($ctrl, CURLOPT_POST, TRUE);
curl_setopt($ctrl, CURLOPT_POSTFIELDS, $transfer);
curl_setopt($ctrl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ctrl, CURLOPT_RETURNTRANSFER, TRUE);
$responce = curl_exec($ctrl);
$responce = json_decode($responce, 0);
curl_close($ctrl);

exit($responce);

?>

If you want to see it on a webserver now the website is being hosted here: https://web.njit.edu/~cfr5/cs490/如果你现在想在网络服务器上看到它,网站托管在这里: https : //web.njit.edu/~cfr5/cs490/

Thanks!谢谢!

类 stdClass 的对象无法在最后一行“exit($responce);”中转换为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM