javascript - 获取 AJAX 响应的特定部分

标签 javascript php jquery ajax html

当我检查 AJAX 请求对 index.php 的响应时,我得到了一些我想要的数据(一些 json,我需要的返回值),而且还返回了作为 index.php 类的 HTML 负载用于调用负责加载一些 HTML 的 View 。

这是响应的前两行:

{"returnVal":"registered"}<!DOCTYPE html>
<html lang="en">

由于我的代码是 MVC,我不能只创建一个单独的文件来处理 AJAX 请求,因此我需要一种方法让我的 login.js 类(生成 AJAX 请求的地方)遍历整个响应并找到我需要的“returnVal”的值。你知道我可以做到这一点的方法吗?

登录.js

var loginData, urlPath;

// Allow users to log in or register
function Login() {

    if(!document.getElementById("usernameField")) { // If we have no username field on this page, we are just logging in
        loginData = "email=" + $("#emailField").val() + "&password=" + $("#passwordField").val() + "&action=" + "loggingIn";
        urlPath = "index.php";
    } else { // we are registering
        loginData = "username=" + $("#usernameField").val() + "&email=" + $("#emailField").val() + "&password=" + $("#passwordField").val() + "&action=" + "register";
        urlPath = "../index.php";
    }

    // Send the login/registration data to database
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: urlPath,
            data: loginData,
            success: function (result) {
                alert(result); // i need to get the value of 'returnVal' from the response

                if(result.returnVal=="registered") {
                    document.getElementById('notification').innerHTML = "You have been registered";
                } else if (result.returnVal=="username") {
                    document.getElementById('notification').innerHTML = "Username already taken";
                } else if (result.returnVal=="email") {
                    document.getElementById('notification').innerHTML = "Email already taken";
                } else if (result.returnVal=="notRegistered") {
                    document.getElementById('notification').innerHTML = "Please enter registered email";
                } else if (result.returnVal=="loginFail") {
                    document.getElementById('notification').innerHTML = "Please enter correct password";
                } else if (result.returnVal=="loggedIn") {
                    $('#myModal').modal('hide');
                    document.getElementById('loginButton').innerHTML = "Account Settings";
                } else { // Something wrong, tell us
                    //alert(result);
                }
            },
            error: function(xhr, status, error) {
                alert(xhr.responseText);
            }

        })
    })
}

index.php

<?php 
    ini_set("log_errors", 1);

    require_once("Model/model.php");
    require_once("Controller/controller.php");
    require_once("View/view.php");

    $model = new Model();
    $view = new View();
    $controller = new Controller($model, $view);

    if(isset($_POST['action'])) {
            if($_POST['action'] == "register") {
                $controller->Register($_POST['username'], $_POST['email'], $_POST['password']);
                echo json_encode($controller->GetReturned());
            }
    }

    $view->Begin();
?>

最佳答案

超简单的方法就是在回显 json 后exit(),这样 View 就永远不会被发送。如果此 Controller 从未打算渲染 View ,请摆脱 $view->Begin();

if(isset($_POST['action'])) {
       if($_POST['action'] == "register") {
             $controller->Register($_POST['username'], $_POST['email'], $_POST['password']);
                echo json_encode($controller->GetReturned());
                exit();
       }
}

关于javascript - 获取 AJAX 响应的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47488897/

相关文章:

javascript - 页面未显示在多页中

jquery - CSS 动画在文本框下方创建填充并移动按钮

javascript - Froala WYISWYG 编辑器不显示工具栏

javascript - 在求和值时组合数组数组的唯一项 - JS/lodash

php - SQL 查询和 PHP 检查用户是否是管理员

php - 准备好的语句是否使用 PHP 跨多个页面加载在服务器端缓存?

javascript - 根据单击的按钮以模态形式设置隐藏输入

javascript - 如何将对象数组转换为 Angular 6 中的对象

javascript - 如何随机分配一个网页图标?

php - api的Laravel多路由文件