【问题标题】:Getting Json data from jquery ajax to php and redirect the page从 jquery ajax 获取 Json 数据到 php 并重定向页面
【发布时间】:2015-12-27 05:00:58
【问题描述】:

我已经使用 Jquery、ajax 和 json 编写了前端简单的登录应用程序,我想将 json 信息发送到 php,它必须检索 json 的数据,如果成功则必须重定向到另一个页面,这是简单的我正在使用的前端代码。

!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="login.css">
        <script src="jquery.js"></script>
        <script>
            $(document).ready(function(){
                $("#submit").on('click',function(){
                    var person={
                    user:$("#user").val(),
                    pass:$("#pass").val(),
                    }
                    $.ajax
                    ({
                        type: "POST",
                        url: '/test/login.php',
                        dataType: 'json',
                        async: false,
                        data: person,
                        success: function () {

                        }
                     })
            });
            });
</script>
    </head>
<body>
    <div id="loginstyle">
<form action="login.php" method="post">
<table>
<tr>
<td>
Email:</td><td><input type="email" name="email" id="user" ></td>
<tr><td>Password:</td><td><input type="password" name="password" id="pass"><td></tr>
</table>
<input type="submit" value="submit" id="submit">
</div>
</form>
</body>
</html>

谁能建议我如何解码 json 数据并打印用户并传入 login.php

【问题讨论】:

    标签: php jquery json ajax


    【解决方案1】:

    你必须这样做

    $.ajax
    ({
         type: "POST",
         url: '/test/login.php',
         dataType: 'json',
         async: false,
         data: person,
         success: function (data) {
               var res = $.parseJSON(data);
               //DO SOMETHING WITH res
               //IF res is some value, redirect, otherwise not.
         }
    });
    

    【讨论】:

    • 我知道,但它在原始问题中,所以我保留了它
    • 我没有前端的问题,我想知道如何在 php 中编写代码,我的意思是检索 json 数据并用一些值验证它,如果你有时间可以告诉我php代码是怎么写的
    • 你可以用json_encode(YOUR_PHP_DATA)将你的php数据编码成json,然后返回。
    • 我在 php 中接收 json 时遇到问题,并从中获取用户和密码,发送我认为将是下一步,我尝试过这样,但我得到空输出
    【解决方案2】:

    1st:您可以在提交时使用e.preventDefault()来防止重新加载页面

    $("#submit").on('click',function(e){
        e.preventDefault();
    

    第二次:当您使用type: "POST", 并在php 中传递user: and pass: 时,您需要

    <?php
       echo($_POST['user']);
       echo($_POST['pass']);
    ?>
    

    以及在ajax成功回调函数

    success: function (data) {
               alert(data);
            }
    

    在这种情况下可能不需要使用 json 作为数据类型

    3rd:看看就好了

    What does "async: false" do in jQuery.ajax()?

    $.ajax - dataType

    encode json using php?

    How to get JSON data on ajax call

    how to parse json data with jquery / javascript?

    【讨论】:

      【解决方案3】:

      当你调用$.ajax,函数时,你定义了一个名为successful的参数,并且你通常给它一个函数,一旦调用是successful就执行它的意思:服务器已经用给定的参数和一个收到了回复。响应可能(或不能)包含在 data 变量中传递的其他数据,您将在调用 successful 函数时使用这些数据。

      这里是棘手的部分:虽然参数称为 successful,但在您的上下文中,提供的凭据可能无效,因此,在您的 PHP 代码中,您将通知 ajax 调用登录尝试是不接受,您可能希望在 data 变量中传递信息。 上面的场景描述了一个不成功的登录尝试,但是一个成功的 ajax 调用。以下是您需要在 javascript 代码中执行的操作,以区分是否应将用户转发到下一页:

      $.ajax({
       type: "POST",
           url: '/test/login.php',
           dataType: 'json',
           async: false,
           data: person,
           success: function (data) {
                 // your php code should populate the (data) variable
                 // in this example I used the variable as a string,
                 // but you could use json notation and have a more 
                 // complex structure (like data.valid = true)
                 if (data == "valid_credentials") {
                      window.location.href = 'nextpage.php';
                 }
                 else
                 {
                     alert('the provided credentials were not accepted.');
                 }
           }
      });
      

      【讨论】:

      • 我没有前端的问题,我想知道如何在 php 中编写代码,我的意思是检索 json 数据并用一些值验证它,如果你有时间可以告诉我php代码是怎么写的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2019-11-12
      • 2017-05-27
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多