dongli8722 2017-11-25 17:48
浏览 57
已采纳

获取AJAX响应的特定部分

When i inspect the response from my AJAX request to index.php, I get back some data that i want (some json, a return value i need the value of) but also a load of HTML as the index.php class is used to call a view which is responsible for loading up some HTML.

Here is the first two lines of the response:

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

Due to my code being MVC, i cannot just create a separate file to handle the AJAX request, so i need a way for my login.js class (where the AJAX request is generated) to go through the whole response and find the value of "returnVal" that I need. Do you know of a way I can do this?

Login.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();
?>
  • 写回答

2条回答 默认 最新

  • drdyszuy488152 2017-11-25 18:11
    关注

    Ultra simple way is just exit() after you echo the json so the view never gets sent. If this controller is never intended to render a view get rid of $view->Begin();

    if(isset($_POST['action'])) {
           if($_POST['action'] == "register") {
                 $controller->Register($_POST['username'], $_POST['email'], $_POST['password']);
                    echo json_encode($controller->GetReturned());
                    exit();
           }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧