简体   繁体   English

在ajax请求后获得响应

[英]get response after ajax request

I am a noob at ajax, but I think my problem is fairly simple. 我是ajax的新手,但是我认为我的问题很简单。 After I submit my form, I would like to have the server echo back to me. 提交表单后,我希望服务器回显给我。 Here is my current code which is not working. 这是我当前的代码不起作用。 I'm fairly certain that the problem is with my ajax, because I know that my values are being placed in dataString. 我相当确定问题出在我的Ajax,因为我知道我的值被放置在dataString中。

HTML 的HTML

<form method="POST" onSubmit="new_user(this);" >
<input type="submit" value="signed" name="astatus" />
<input type="hidden" value="FaKEIdkEy" name="mark_attend" />
</form>

Javascript Java脚本

function new_user(form){
    var url = "attendance.php";

    var dataString = "status="+form.astatus.value;
    dataString = dataString+"&id="+form.mark_attend.value;

    $.ajax({  
type: "POST",  
url: url, 
data: dataString,  
success: function(data) {
alert(data);
}
});
}

php (attendance.php) php (attendance.php)

if(!empty($_POST))
{
echo "response";
}

Any Ideas on how to fix? 关于如何解决的任何想法?

You should add a return false; 您应该添加return false; at the end of the function to prevent the form from submitting. 在函数的末尾阻止提交表单。

function new_user(form) {
    var url = "attendance.php";

    var dataString = "status="+form.astatus.value+"&id="+form.mark_attend.value;

    $.ajax({  
        type: "POST",  
        url: url, 
        data: dataString,  
        success: function(data) {
            alert(data);
        }
    });

    return false;
}
$.ajax({  
    type: "POST",  
    url: url, 
    data: dataString,  
    success: function(data) {
        console.log(data);
    }
});

Does the console show "response"? 控制台是否显示“响应”?

You are calling ajax at on submit , so when ajax run after that page is reload so data is vanished. 
<form method="POST"  >
<input type="button" value="signed" name="astatus" onclick="new_user()" />
<input type="hidden" value="FaKEIdkEy" name="mark_attend"  id="mark_attend"/>
</form>

and get the value with jquery 
var fakeidkey=$_('#mark_attend').val();
and then concat it with your varaible .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM