gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:59:08 26 4
gpt4 key购买 nike

当我检查 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com