gpt4 book ai didi

javascript - 在 zend 框架 2 中使用 ajax

转载 作者:数据小太阳 更新时间:2023-10-29 06:11:58 24 4
gpt4 key购买 nike

我对 zend 框架 2 和 Web 应用程序编程真的很陌生。在我的应用程序中,我想要一个按钮来触发一个函数来更改数据库的内容并返回一个字符串,我可以用它来更新网站的可见内容。因为我不希望网站在单击按钮时重新加载,所以我想使用 ajax 来完成此操作。在阅读了几个 ajax 教程之后,我想象解决方案看起来与此类似:

HTML 部分:

 <head>

<script type="text/javascript">

function myFunction() {

var xmlhttp = new XMLHttpRequest();
// I am working with Chrome

xmlhttp.onreadystatechange=function(){

if (xmlhttp.readyState == 4 && xmlhttp.status == 200){

var text = xmlhttp.responseText;
document.getElementById("text_paragraph").innerHTML =
text;
}
};

xmlhttp.open("GET", "function.php", true);
xmlhttp.send();

}

</script>

</head>

<body>
......
<button id="function_button" onClick="myFunction()">Click</button>
<p id = "text_paragraph">Initial text"</p>
......
</body>

使用 .php 文件 function.php(一开始,我只希望它返回一个文本值):

<?php

echo "Text triggered by the button click";
?>

当我尝试测试按钮时,没有任何反应。显然,xmlhttp.status 是 404 并且找不到 function.php 文件。我想我放置 function.php 文件的位置(它与 .phtml 在同一文件夹中 - 网站的 View 文件)或我在 xmlhttp.open - 函数中使用的 url 是错误的。你能告诉我如何在 zf2 中正确使用 ajax 吗?感谢您抽出时间,非常感谢您的回答。

最佳答案

首先,我要感谢您的所有回答。他们真的帮了大忙。使用 jQuery 确实比使用纯 Javascript 舒服得多,而且不仅仅是 Ajax 调用。詹姆斯,非常感谢你的回答。我尝试使用它,但我可能做错了什么,因为它不起作用。我为我的问题找到了另一种解决方案,我想将其发布在这里以解决这个问题。

我要发布的代码是一个小例子,它只是做一件简单的事情:用户点击由 zend framework 2 创建的网站中的按钮,输入字段被读取,其内容被传输到一个服务器函数,它根据它接收到的数据返回一个特定的结果。收到结果后,无需重新加载即可更新网站。

由于我将使用 Json 对象进行通信,因此有必要通过将以下代码行添加到 module.config.php 来激活 zf2 中的 Json 策略:

// module/Application/config/module.config.php

'view_manager' => array (
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array (
'layout/layout' => __DIR__ .
'/../view/layout/layout.phtml',
'application/index/index' => __DIR__ .
'/../view/application/index/index.phtml',
'error/404' => __DIR__ .
'/../view/error/404.phtml',
'error/index' => __DIR__ .
'/../view/error/index.phtml'
),
'template_path_stack' => array (
__DIR__ . '/../view'
),
'strategies' => array ( // Add
// this
'ViewJsonStrategy' // line
)

),

网站的 View 文件(例如名为 example.phtml)看起来类似于:

<html>
<head>

<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script>

//Function, activated by clicking the button

$("#trigger").click(function(){

var text = $("#input_to_read").val();
var myData = {textData:text};

//The post using ajax
$.ajax({
type:"POST",
// URL : / name of the controller for the site / name of the action to be
// executed
url:"/example/answer",
data:myData,
success: function(data){
//The callback function, that is going to be executed
//after the server response. data is the data returned
//from the server.

// Show the returned text
$("#answer").text(data.text);


},
failure(function(){alert("Failure!!");})


});


});


</script>
</head>

<body>

<input id="input_to_read" type="text">
<button id="trigger">Klick</button>
<!-- The answer will be shown here. -->
<p id="answer"></p>
</body>
</html>

单击按钮时将调用的服务器函数放置在网站的 Controller 中(在本例中为 ExampleController),看起来像这样:

//Make sure to add this line to use Json objects
use Zend\View\Model\JsonModel;

....


public function answerAction(){

#read the data sent from the site
$text = $_POST['textData'];

#do something with the data

$text = $text . "successfully processed";

#return a Json object containing the data

$result = new JsonModel ( array (

'text' => $text
) );
return $result;
}

关于javascript - 在 zend 框架 2 中使用 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19054288/

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