Dojo,xhrPost不能在对话框中使用程序创建的表单

Dojo, xhrPost does not work with programmatically created form in a dialog

本文关键字:程序 创建 表单 xhrPost 不能 对话框 Dojo      更新时间:2024-01-20

我正在对话框中"以编程方式"创建一个表单,当dom准备好时,它会正确显示。单击"Login"按钮,POST请求被正确触发(我正在检查chrome调试器),但是,POST请求似乎不包含任何表单数据。响应中没有usernameF和passwordF参数。如果我尝试将xhrPost与以前(即静态)实例化的表单(HTML,就像网上的所有示例一样)一起使用,那么问题就不存在了。原因是什么?

这是代码:

require([
    "dijit/Dialog",
    "dijit/form/Form",
    "dijit/form/TextBox",
    "dijit/form/Button",
    "dojo/domReady!",
], function(Dialog, Form, TextBox, Button)
{
    var form = new Form({id: "loginformF"});
    var usernameF = new TextBox({
        id: "usernameF",
        placeHolder: "Username"
    });
    usernameF.placeAt(form.containerNode);
    var passwordF = new TextBox({
        id: "passwordF",
        placeHolder: "Password",
        type: 'password'
    });
    passwordF.placeAt(form.containerNode);
    new Button({
        id: "login",
        label: "Login",
        onClick: function(event) {
            //chiamata ajax
            dojo.xhrPost({
                url: "login.php",
                form: form.containerNode,
                load: function(data) {
                    console.log("Message posted.");
                },
                error: function(error) {
                    console.log("Message posted.");
                }
            });
            console.log("Message being sent...");
        }
    }).placeAt(form.containerNode);
    //crea il dialog
    var dia = new Dialog({
        content: form,
        title: "Dialog with form"
    });
    //form.startup();
    dia.show();
});

以下是从Chrome调试器获取的请求/响应的转储,正如您所看到的,没有"表单数据"部分:

Request URL:http://localhost/cv.dojo/login.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:it,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:0
Content-Type:application/x-www-form-urlencoded
Host:localhost
Origin:http://localhost
Referer:http://localhost/cv.dojo/?
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.67 Safari/537.36
X-Requested-With:XMLHttpRequest
Response Headersview source
Connection:Keep-Alive
Content-Length:10
Content-Type:text/html
Date:Mon, 02 Dec 2013 18:31:38 GMT
Keep-Alive:timeout=5, max=96
Server:Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19
X-Powered-By:PHP/5.4.19

我希望在回复中找到这样的部分:

Form Dataview sourceview URL encoded
usernameF:blablablablabla
passwordF:blablablablabla

怎么了?

该死!:)明白了!

我只是忘记了输入字段中的"name"参数。

这是更正后的代码,看看

名称:"用户名F",

名称:"passwordF",

require([
    "dijit/Dialog",
    "dijit/form/Form",
    "dijit/form/TextBox",
    "dijit/form/Button",
    "dojo/domReady!",
], function(Dialog, Form, TextBox, Button)
{
    var form = new Form({id: "loginformF"});
    var usernameF = new TextBox({
        id: "usernameF",
        name:"usernameF",
        placeHolder: "Username"
    });
    usernameF.placeAt(form.containerNode);
    var passwordF = new TextBox({
        id: "passwordF",
        name:"passwordF",
        placeHolder: "Password",
        type: 'password'
    });
    passwordF.placeAt(form.containerNode);
    new Button({
        id: "login",
        label: "Login",
        onClick: function(event) {
            //chiamata ajax
            dojo.xhrPost({
                url: "login.php",
                form: form.containerNode,
                load: function(data) {
                    console.log("Message posted.");
                },
                error: function(error) {
                    console.log("Message posted.");
                }
            });
            console.log("Message being sent...");
        }
    }).placeAt(form.containerNode);
    //crea il dialog
    var dia = new Dialog({
        content: form,
        title: "Dialog with form"
    });
    //form.startup();
    dia.show();
});

无论如何,谢谢。