AJAX 请求响应
AJAX request response
我是 AJAX 的新手。我正在发送一个请求,要求在另一个页面上启动一个 PHP 代码,其中有几件事需要发生。我在那个页面上有一个算法来检查所有的事情是否按正确的顺序正确地完成了。我需要的是 return 那个布尔值回到 AJAX 文件,这样它就会知道请求不仅被接收,而且以预期的方式完成,这样我就可以添加一个 成功: 函数,可以给用户带来 OK/NEY 体验。
你可以在 AJAX 的 success 函数中 return(true 表示成功,false 表示不成功)变量。在此基础上,您可以进行下一步。
虚拟示例:
$.ajax({
url: '',
type:'POST',
data: {'id':id},
dataType:'json',
beforeSend:function(){
},
error : function(xhr, textStatus, errorThrown) {
alert('An error occurred!');
},
success:function(response){
if(response){
console.log('yes');
}
else{
console.log('no');
}
}
complete: function() {
}
});
您的文件夹:
AnyFolderName
|--- index.php
|--- script.php
Index.php:
<!DOCTYPE html>
<html>
<head>
<title>AJAX Request Sample</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
function submitform(){
//get value of input whos id is username
var username = $('input#username').val();
//data to post to script.php
var post = {username: username};
$.ajax({
url: "script.php", //<-- PHP Script where you want to post your data
type: "POST",
data: post,
success: function(data){
//callback when request is finished.
//in our script.php, we just echoed the posted username, so it alerts whatever username you have input.
alert(data);
}
});
}
</script>
</head>
<body>
<form id="myForm">
<input id="username" name="username">
</form>
<button onclick="submitform();"></button>
</body>
</html>
script.php:
<?php
//print out the posted data you have passed from the AJAX request
echo $_POST['username'];
?>
哦,这是我的错误。浏览了几十页,但最终在另一个线程上找到了答案(所以我的有点重复):
How to return data from PHP to a jQuery ajax call
必须使用echo();而不是 return();在 PHP 文件中!
当然,还有相应的数据类型,在本例中为 "text".
这是我的解决方案....
$.ajax(
{
类型:'POST'、
url: '../some/file/file.php',
async: true, // 这很重要!!!
成功:函数(响应)
{
如果(响应)
{
//使用本地环境存储来保存数据
localStorage.setItem('data',响应);
}
}
});
//请求数据
some_variable = localStorage.getItem('data').trim(); //trim很重要!!!
玩得开心!!!!
我是 AJAX 的新手。我正在发送一个请求,要求在另一个页面上启动一个 PHP 代码,其中有几件事需要发生。我在那个页面上有一个算法来检查所有的事情是否按正确的顺序正确地完成了。我需要的是 return 那个布尔值回到 AJAX 文件,这样它就会知道请求不仅被接收,而且以预期的方式完成,这样我就可以添加一个 成功: 函数,可以给用户带来 OK/NEY 体验。
你可以在 AJAX 的 success 函数中 return(true 表示成功,false 表示不成功)变量。在此基础上,您可以进行下一步。
虚拟示例:
$.ajax({
url: '',
type:'POST',
data: {'id':id},
dataType:'json',
beforeSend:function(){
},
error : function(xhr, textStatus, errorThrown) {
alert('An error occurred!');
},
success:function(response){
if(response){
console.log('yes');
}
else{
console.log('no');
}
}
complete: function() {
}
});
您的文件夹:
AnyFolderName
|--- index.php
|--- script.php
Index.php:
<!DOCTYPE html>
<html>
<head>
<title>AJAX Request Sample</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
function submitform(){
//get value of input whos id is username
var username = $('input#username').val();
//data to post to script.php
var post = {username: username};
$.ajax({
url: "script.php", //<-- PHP Script where you want to post your data
type: "POST",
data: post,
success: function(data){
//callback when request is finished.
//in our script.php, we just echoed the posted username, so it alerts whatever username you have input.
alert(data);
}
});
}
</script>
</head>
<body>
<form id="myForm">
<input id="username" name="username">
</form>
<button onclick="submitform();"></button>
</body>
</html>
script.php:
<?php
//print out the posted data you have passed from the AJAX request
echo $_POST['username'];
?>
哦,这是我的错误。浏览了几十页,但最终在另一个线程上找到了答案(所以我的有点重复):
How to return data from PHP to a jQuery ajax call
必须使用echo();而不是 return();在 PHP 文件中! 当然,还有相应的数据类型,在本例中为 "text".
这是我的解决方案....
$.ajax(
{
类型:'POST'、
url: '../some/file/file.php',
async: true, // 这很重要!!!
成功:函数(响应)
{
如果(响应)
{
//使用本地环境存储来保存数据
localStorage.setItem('data',响应);
}
}
});
//请求数据
some_variable = localStorage.getItem('data').trim(); //trim很重要!!!
玩得开心!!!!