【问题标题】:Cannot pass the object into the javascript function as a parameter无法将对象作为参数传递给 javascript 函数
【发布时间】:2019-08-29 17:36:58
【问题描述】:

我正在为网站构建用户注册。它使用 AJAX 发送数据。我创建了一个对象来存储用户数据并通过函数将对象发送到服务器。

sendUserData(passenger, "user_registration.php");

注意:

  • 在 HTML 文档中单击按钮调用 signupUser();
  • sendUserData()是发送用户详细信息的函数。
  • passenger 是存储用户数据的对象。
  • sendUserData(); 位于链接到 HTML 页面的单独脚本中。(全局范围)

这是我用来做这个的代码

//Sign Up A User

var signupUser = function () {

// User Object

var passenger = {       
 userName : document.getElementById("userFirstName").value,
 userEmail : document.getElementById("userEmail").value,
 country : document.getElementById("country").value,
 password : document.getElementById("userPassword").value,
 passwordVerify : document.getElementById("userPasswordVerify").value,
 passwordStatus : Boolean
};

// Verifies the password ("password" and "re-enter password")

verifyPassword(passenger.password,passenger.passwordVerify,passenger.passwordStatus);

if (passenger.passwordStatus == true) {

    sendUserData(passenger, "user_registration.php");
    console.log(passenger);
}

};

问题在于在按钮上调用函数signupUser(), 函数在verifyPassword(); 之后停止。

sendUserData(passenger, "user_registration.php");在chrome调试工具中调用,报错“passenger is not defined at : 1:13”

请帮我找到答案。 谢谢。

编辑:

sendUserData();函数

这是在链接到 HTML 页面的单独 javascript 文件中。


function sendUserData (userDetails, destinationPage, functionToExecute) {


         var userDetailsJson = JSON.stringify(userDetails);
         var xhr = new XMLHttpRequest();
         xhr.onreadystatechange = function () {
          if(xhr.readyState == 4 && xhr.status == 200 ) {
           console.log(userDetailsJson);
           window.alert("Successfully Posted");
           console.log(xhr.responseText);

           if(functionToExecute) {

           functionToExecute();
           }

             }
         };

         xhr.open("POST", destinationPage, false);
         xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         xhr.send("m=" +userDetailsJson);
     }

【问题讨论】:

  • 你能显示sendUserData函数的代码吗?
  • 是的。我会将其添加到问题的末尾。

标签: javascript html scope


【解决方案1】:

所以这取决于passenger.passwordStatus == true 没有成功。

你将它初始化为passwordStatus: Boolean,它已经不像你认为的那样做了。

然后你将参数传递给verifyPassword 函数,大概看起来像这样:

function verifyPassword( password, retype, status) {
    // verify...
    status = true;
}

重新分配 status 并且根本不影响外部范围内的passenger.passwordStatus

因此,在检查状态时,您仍在比较Boolean == true,这永远不会起作用。

你可能想要更多类似的东西:

if( verifyPassword( passenger.password, passenger.passwordVerify)) {
    sendUserData(...);
}

verifyPassword 根据需要执行return true;return false;

彻底摆脱passenger.passwordStatus这个东西。

【讨论】:

  • 你能解释一下passenger.passwordStatus 的不工作吗?我还不是很清楚。
  • var verifyPassword = function (password,passwordVerify,user) { if (password === passwordVerify) { user = true; console.log(user); } } 这是 verifyPassword 函数。
  • 您可以完全摆脱该功能,只需执行if(passenger.password == passenger.passwordVerify) { sendUserData(...); }
  • @NiettheDarkAbsol 感谢您的支持。无论如何,我找到了一种使用它的方法,将函数内部的user 变成user.passwordStatus 就可以了。对不起,我没有忽视你的建议。只是想找到解决这个问题的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 2018-02-23
  • 2012-05-30
  • 1970-01-01
  • 2021-04-15
  • 2018-03-15
  • 1970-01-01
相关资源
最近更新 更多