获取使用AJAX的PHP文件响应 [英] Get response from PHP file using AJAX
问题描述
因此,这里是我的问题,我使用AJAX(jQuery的),以张贴形式 process.php
但页面实际需要呼应了回应,如苹果
或梅
。我不知道如何采取从 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...
这里的code我到目前为止有:
Here's the code I have so far:
<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?
抱歉,如果这听起来像一个愚蠢的问题,这是我第一次做一些这样的阿贾克斯。
Sorry if this sounds like a stupid question, this is my first time doing something as such in Ajax.
PS:您好,我的名字在上面code中的POST请求
PS: How do I name the POST request in the above code?
推荐答案
&LT;?PHP的回声苹果; ?&GT;
是pretty的太多字面上你需要的所有服务器上。
<?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屋!