使用 AJAX 从 PHP 文件获取响应 [英] Get response from PHP file using AJAX
问题描述
所以这是我的问题,我使用 AJAX (jQuery) 将表单发布到 process.php
但页面实际上需要回显响应,例如 apple
或 梅花
.我不确定如何从 process.php
获取响应并将其存储为变量...
So here's my issue, I am using AJAX (jQuery) to post a form to process.php
but the page actually needs to echo out a response such as apple
or plum
. I'm not sure how to take the response from process.php
and have it stored as a variable...
这是我目前的代码:
<script type="text/javascript">
function returnwasset(){
alert('return sent');
$.ajax({
type: "POST",
url: "process.php",
data: somedata;
success function(){
//echo what the server sent back...
}
});
}
</script>
我还需要在 json 中回显 process.php
中的响应吗?还是纯文本可以?
Also will I need to echo the response in process.php
in json? or will plain text be fine?
对不起,如果这听起来像一个愚蠢的问题,这是我第一次在 Ajax 中做这样的事情.
Sorry if this sounds like a stupid question, this is my first time doing something as such in Ajax.
PS:上面代码中的POST请求如何命名?
PS: How do I name the POST request in the above code?
推荐答案
几乎是您在服务器上所需的全部内容.
<?php echo 'apple'; ?>
is pretty much literally all you need on the server.
至于 JS 端,服务器端脚本的输出作为参数传递给成功处理函数,所以你有
as for the JS side, the output of the server-side script is passed as a parameter to the success handler function, so you'd have
success: function(data) {
alert(data); // apple
}
这篇关于使用 AJAX 从 PHP 文件获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!