拯救XMLHttpRequest.Javascript变量中的responseText
save XMLHttpRequest.responseText in a Javascript variable
这个问题之前被问了2次,我无法让它工作。在我阅读了save - XMLHttpRequest -responsetext-in-a-variable和store-xmlhttprequest-responsetext-as-variable两篇文章之后,我了解了如何调用回调函数,因为XMLHttpRequest是异步的。(似乎是显而易见的)
因此,我创建了我的phpfile checkemail。php:<?php
if(isset($_POST['email'])){ //if we get the email sucessfully
$email = mysqli_real_escape_string($db, $_POST['email']);
if(!empty($email)){
$email_query = mysqli_query($db, "SELECT COUNT(`email`) FROM `users` WHERE `email`='$email'");
$email_result = mysqli_fetch_row($email_query);
if($email_result[0]==0){
//haven't found the email in the DB
echo "0";
} else{
//found the email in the DB!
echo "1";
}
}
}
?>
所以我的结果是0或1,无论电子邮件是否已经在数据库中。
给我的Javascript代码和我的问题这个线程:
function callback(responseText){
var save = responseText;
return save;
}
function checkemail(email){
var hr = new XMLHttpRequest();
hr.open("POST", "./inc/func/checkemail.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Send the data to PHP
hr.send('email='+email);
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
console.log(hr);
console.log(hr.responseText);
callback(hr.responseText);
}
}
}
有趣的是,继承函数'onreadystatechange'中的console.log(hr.responseText);
给了我绝对正确的结果。
但是,当我调用函数checkemail('test@test.com')
函数回调不调用,或者我误解了一些东西吗?
帮助将是非常感激的。提前感谢
我也尝试过这个,因为它对我来说更合乎逻辑(当同一块代码中的console.log已经给了我正确的结果…):
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
console.log(hr);
console.log(hr.responseText);
return hr.responseText;
}
}
在另一篇文章中,我看到了语句
如果你改变你的函数调用
hr.open("GET", url, false);
,这应该使你的调用同步和JavaScript将等待,直到收到响应。
当然,正如我之前说过的,我想调用if(checkemail('test@test.com')=="1")
…这个建议会有帮助吗?到目前为止,我还不能让它工作。可悲的是:(
现在@Hawk给了我一些提示,我已经尝试了更多的方法来让我的函数返回真或假。
但是我想知道如果我的函数不能调用函数callback
function callback(responseText){
var save = responseText;
return save;
}
function checkemail(email){
var email = email;
var hr = new XMLHttpRequest();
hr.open("POST", "./inc/func/check_email.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Send the data to PHP
hr.send('email='+email);
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
email=hr.responseText;
//return email
document.getElementById('status').innerHTML="email is used already: "+email;
return callback(email);
}
//return callback(email);
}
}
在函数checkemail之外定义函数回调是否有区别,或者我必须在函数checkemail中定义它,就像这样:
function checkemail(email){
function callback(responseText){
var save = responseText;
return save;
}
var email = email;
var hr = new XMLHttpRequest();
hr.open("POST", "./inc/func/check_email.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Send the data to PHP
hr.send('email='+email);
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
email=hr.responseText;
//return email
document.getElementById('status').innerHTML="email is used already: "+email;
return callback(email);
}
//return callback(email);
}
}
try this
function checkemail(email)
{
var email = email;
//code
if( hr.readystatechange == 4 & hr.status == 200 )
{
email = hr.responseText;
}
}
这样email就会改变并存储在函数作用域中的email中,并在函数执行时保持在那里,希望它能有所帮助:)
尝试将调用回调(hr.responsetext)的部分更改为此
返回回调(hr.responseText);
,这样你返回一个函数,反过来返回保存…我祈祷工作…:)
请让我知道它是否有效
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 如何通过ajax刷新JSF填充的javascript变量
- 参数变量出现ngTable指令问题
- 通过javascript重定向html传递php变量
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 全局变量和全局对象的属性之间有什么区别吗
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 值对象在某个变量发生更改后发生更改
- Javascript变量赋值|
- 将 XMLHttpRequest.responseText 存储到变量中
- PHP to JS:打印与 AJAX responseText 兼容的 js 变量(如 PHP echo)
- 在javascript responseText中将临时变量声明为全局变量
- 在.js文件中,我可以在处理ajax responsetext后更新一个变量吗
- Ajax . responsetext在alert中可用,但不会保存在变量或return中
- 拯救XMLHttpRequest.Javascript变量中的responseText
- 将XMLHttpRequest.responseText保存在变量中
- 如何从$中分配数据.获取一个变量,或者读取responseText