简体   繁体   中英

JS Script Form Post not showing up for ASP.NET Request.Form

I have several User Controls and one of which requires a click to go to a different page. I have a javascript function that is called and with the alert within, I know it's posting the correct data. Also this Javascript call is used with other websites and is working fine, but they are not dealing with user controls. The user control, calls ShowNotes, but posting to a normal form page with a site.master page. I'm thinking I'm missing something simple, so please be kind and let me know what I'm missing.

function ShowNotes(appName, serverName, appDataID) {
    var params = [];
    params.push({
        AppName: appName,
        ServerName: serverName,
        AppDataID: appDataID
    });
    formPost("/Notes.aspx", params);
}

function formPost(url, params) {
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("name", "hiddenForm");
    form.setAttribute("action", url);
    var hiddenField;
    for (var key in params) {
        for (var val in params[key]) {
            hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", val);
            hiddenField.setAttribute("value", params[key][val]);

            form.appendChild(hiddenField);
            //alert(val + ": " + params[key][val]);
        }
    }

    document.body.appendChild(form);
    form.submit();
}

Then on the Notes.aspx side, I debug and show the Request.Form.Count = 0. What can I possibly be missing? I also looked at the request.QueryString and it's empty.

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.Form.Count > 0)
            {

EDIT:

Based on VDWWD comment, I modified the method to check if a form exists and use that one. I'm still getting the same results.

function formPost(url, params) {
    var form = document.forms[0];
    if (form == undefined) {
        form = document.createElement("form");
        form.setAttribute("name", "hiddenForm");
        form.setAttribute("id", "hiddenForm");
    }
    alert(form.id);
    form.setAttribute("method", "post");
    form.setAttribute("action", url);
    var hiddenField;
    for (var key in params) {
        for (var val in params[key]) {
            hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", val);
            hiddenField.setAttribute("value", params[key][val]);

            form.appendChild(hiddenField);
            //alert(val + ": " + params[key][val]);
        }
    }

    document.body.appendChild(form);
    form.submit();
}

I started researching this 301 Moved Permanently and found that people stating, to change the post to just the page without the extension. The 301 is redirecting the page without the extension, causing the POST data to not be passed.

So I changed my JavaScript to remove the .aspx and put a slash in it's place and it works, even through it's a ASPX page:

function ShowNotes(appName, serverName, appDataID) {
    var params = [];
    params.push({
        AppName: appName,
        ServerName: serverName,
        AppDataID: appDataID
    });
    formPost("/Notes/", params);
}

I tested the following simplified snippet from your edit after my comment.

<script type="text/javascript">
    function formPost(url, params) {
        var form = document.forms[0];
        form.setAttribute("method", "post");
        form.setAttribute("action", '/default.aspx');
        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", 'FormPostTest');
        hiddenField.setAttribute("value", "TheValueOfTheHiddenField");
        form.appendChild(hiddenField);
        form.submit();
    }
</script>

And on Default.aspx I retrieve the value with this Request.Form["FormPostTest"] and it works just fine. Even if the post page is different from the page the form is on.

Are you sure you are posting to the correct page and with the correct name ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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