AJAX请求响应
我是AJAX新手。我正在发送一个请求,要求在另一个页面上启动一个PHP代码,在该页面上需要发生两件事情。我在那页上有一个算法,检查所有的事情是否按正确的顺序正确完成。我需要的是将该布尔值返回到AJAX文件中,这样它就知道请求不仅已收到,而且已按预期方式完成,这样我就可以添加一个success:函数,为用户提供OK/NEY体验。您可以在AJAX的success函数中返回(true表示成功或false表示不成功)变量。在此基础上,你可以继续下一步 虚拟示例:AJAX请求响应,ajax,request,response,Ajax,Request,Response,我是AJAX新手。我正在发送一个请求,要求在另一个页面上启动一个PHP代码,在该页面上需要发生两件事情。我在那页上有一个算法,检查所有的事情是否按正确的顺序正确完成。我需要的是将该布尔值返回到AJAX文件中,这样它就知道请求不仅已收到,而且已按预期方式完成,这样我就可以添加一个success:函数,为用户提供OK/NEY体验。您可以在AJAX的success函数中返回(true表示成功或false表示不成功)变量。在此基础上,你可以继续下一步 虚拟示例: $.ajax({
$.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() {
}
});
您可以在AJAX的success函数中返回(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>
AJAX请求示例
函数submitform(){
//获取id为用户名的输入的值
var username=$('input#username').val();
//要发布到script.php的数据
var post={username:username};
$.ajax({
url:“script.php”,//您的文件夹:
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>
AJAX请求示例
函数submitform(){
//获取id为用户名的输入的值
var username=$('input#username').val();
//要发布到script.php的数据
var post={username:username};
$.ajax({
url:“script.php”,//哦,这是我的错误。我浏览了几十页,但最终在另一个线程上找到了答案(因此我的答案有点重复):
必须在PHP文件中使用echo();而不是return()!
当然,还有相应的数据类型,在本例中是“text”。哦,这是我的错误。我浏览了几十页,但最终在另一个线程上找到了答案(因此我的是一种重复):
必须在PHP文件中使用echo();而不是return()!
当然,还有相应的数据类型,在本例中为“text”。这是我的解决方案……
$.ajax(
{
键入:“POST”,
url:“../some/file/file.php”,
async:true,//这很重要!!!
成功:功能(响应)
{
如果(响应)
{
//使用本地环境存储来保存数据
localStorage.setItem('data',response);
}
}
});
//请求数据
some_variable=localStorage.getItem('data').trim();//trim很重要!!!
玩得开心!!!!这是我的解决方案……
$.ajax(
{
键入:“POST”,
url:“../some/file/file.php”,
async:true,//这很重要!!!
成功:功能(响应)
{
如果(响应)
{
//使用本地环境存储来保存数据
localStorage.setItem('data',response);
}
}
});
//请求数据
some_variable=localStorage.getItem('data').trim();//trim很重要!!!
玩得开心!!是的,这正是我在PHP文件中最后做的。这是我的请求不起作用的第二个问题。是的,这正是我在PHP文件中最后做的。这是我的请求不起作用的第二个问题。感谢数据类型建议。我浪费了很多时间,不知道该参数,当我查看时它似乎不支持布尔值。从那时起,我开始传递是/否字符串。传递真/假在php编码中意味着1/Null感谢数据类型建议。我浪费了很多时间,不知道那个参数,当我查找它时,它似乎不支持布尔值。从那时起,它就开始了coming在一起,当我传递yes/no strings.pass true/false时,它在php编码中的意思是1/null