0

我正在尝试使用dojo.xhrPost. 下面的代码在 chrome 中运行良好,但在 Firefox 中根本不起作用。当我说它不起作用时,我看到该页面再次重新加载并且没有任何反应。我尝试使用dojo.stopEvent(event);但似乎无法在 Firefox 中使用。

你能建议我什么可能是我的错误。我觉得问题更多在于表单而不是 xhrPost。

HTML 如下所示:

<div data-dojo-type="dijit/form/Form" data-dojo-id="myform" id="loginform" 
encType="multipart/form-data" action="" method="post">
<script type="dojo/method" data-dojo-event="onSubmit">
    if(this.validate()){
        senddata();  //calling the javascript function
    }else{
        alert('Form contains invalid data.  Please correct first');
        return false;
    }
    return true;
 </script>
  <table cellspacing="10">
  <tr>
  <td><label for="name">Username:</label></td>
  <td><input type="text" id="username" name="username" 
  required="true" placeholder="Your UserName" data-dojo-
  type="dijit.form.ValidationTextBox"/></td>
  </tr>
  <tr>
  <td><label for="password">Password:</label></td>
  <td><input type="password" id="password" name="password"       
  required="true" placeholder="Your Password" 
  data-dojotype="dijit.form.ValidationTextBox"/></td>
  </tr>
 </table>
 <div id="response" style="float: right"></div>
 <button data-dojo-type="dijit.form.Button" type="submit" name="submitButton" 
    value="Submit" style="float: right;">Submit</button>
 </div>

我的Javascript代码如下:

function senddata(){
dojo.stopEvent(event);

obj = {};
obj.user_name =dijit.byId("username").get("value");
obj.password =dijit.byId("password").get("value");

var xhrArgs = {
          url: "./script/php/validatelogin.php",
          postData: obj,
          handleAs: "json",
          load: function(data){
              //alert('success');
              if(data.success==true){
                  window.location = data.message;
                  dojo.byId("response").innerHTML = "Form posted.";  
              }else{
                  dojo.byId("response").innerHTML = "login Failed";
              }

          },
          error: function(error){
             console.log("error occured!!!");
             dojo.byId("response").innerHTML = "Failed to Post the Form..";
                }
          };


//alert('starting');
 var deferred = dojo.xhrPost(xhrArgs);
 //alert('done');

 return false;

}
4

1 回答 1

0

首先,更改data-dojo-type成路径格式。例如:dijit/form/ValidationTextBox

其次,修正您在密码字段中的输入错误data-dojo-type。您输入了'dojo' 和 'type' 之间data-dojotype的b=in-

有关 dojo.byId() 和 dijit.byId() 的更多信息。

结果如下:

    <div data-dojo-type="dijit/form/Form" data-dojo-id="myform" id="loginform" encType="multipart/form-data" action="" method="post">
    <script type="dojo/method" data-dojo-event="onSubmit">
        if(this.validate()){
            senddata();  //calling the javascript function
        }else{
            alert('Form contains invalid data.  Please correct first');
            return false;
        }
        return true;
    </script>

    <table cellspacing="10">
        <tr>
            <td><label for="name">Username:</label></td>
            <td><input type="text" id="username" name="username" required="true" placeholder="Your UserName" data-dojo-type="dijit/form/ValidationTextBox" /></td>
        </tr>
        <tr>
            <td><label for="password">Password:</label></td>
            <td><input type="password" id="password" name="password" required="true" placeholder="Your Password" data-dojo-type="dijit/form/ValidationTextBox" /></td>
        </tr>
    </table>
    <div id="response" style="float: right"></div>
    <button data-dojo-type="dijit/form/Button" type="submit" name="submitButton" value="Submit" style="float: right;">Submit</button>
</div>

<script>
function senddata(){
    dojo.stopEvent(event);

    obj = {};
    obj.user_name =dijit.byId("username").get("value");
    obj.password =dijit.byId("password").get("value");

    var xhrArgs = {
        url: "./script/php/validatelogin.php",
        postData: obj,
        handleAs: "json",
        load: function(data){
            //alert('success');
            if(data.success==true){
                window.location = data.message;
                dojo.byId("response").innerHTML = "Form posted.";  
            }else{
                dojo.byId("response").innerHTML = "login Failed";
            }
        },
        error: function(error){
            console.log("error occured!!!");
            dojo.byId("response").innerHTML = "Failed to Post the Form..";
        }
    };
    //alert('starting');
    var deferred = dojo.xhrPost(xhrArgs);
    //alert('done');
    return false;
}
</script>
于 2013-08-01T07:06:46.627 回答