获取AJAX响应的特定部分

时间:2017-11-25 17:48:02

标签: javascript php jquery ajax html5

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

以下是回复的前两行:

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

由于我的代码是MVC,我不能只创建一个单独的文件来处理AJAX请求,所以我需要一种方法来我的login.js类(生成AJAX请求)来完成整个响应并查找&#34; returnVal&#34;的值我需要的。你知道我能做到这一点吗?

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 个答案:

答案 0 :(得分:1)

在回显json之后,超简单的方法只是exit(),因此视图永远不会被发送。如果此控制器永远不打算渲染视图,请删除$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 :(得分:0)

这是一种(杂乱但仍然)提取所需数据的方式 但请考虑我的第一个评论。你应该反过来做。

var result = '{"returnVal":"registered"}<!DOCTYPE html>someother grap';
var n = result.indexOf("<!DOCTYPE");
var jsonString = input.substring(0, n);
var json = JSON.parse(jsonString);
console.log(json);
// your values are here:
// json.returnVal;

这取决于严格的惯例,即每个回报都有一个&#39;