简体   繁体   中英

Get POST values from an Ajax request for ASP.Net

I'm pulling my hair out. I can't get this simple thing to work. I can't get the values from POST? What's the trick to reading POST values within aspx pages.

Here is the html page.

                <html>
                <head>
                </head>
                <body>
                    <form id="frm_post" action="default.aspx" method="POST">
                    <table>
                        <tr>
                            <td>
                                Name 2:
                            </td>
                            <td>
                                <input type="text" id="txtName2″" name="name2″" value="Jack" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Address 2:
                            </td>
                            <td>
                                <input type="text" id="txtAddr2″" name="addr2″" value="Oz" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td>
                                <input type="submit" value="Send Using Post" />
                            </td>
                        </tr>
                    </table>
                    </form>
                </body>
                </html>

Here is my aspx pge

            protected void Page_Load(object sender, EventArgs e)
            {


                if (Request.HttpMethod == "POST")
                {
                    string text = Request.Form["name2"];
                    Response.Output.WriteLine(text);
                }

            }

something like this...

NameValueCollection nvc = Request.Form;
string userName, password;
if (!string.IsNullOrEmpty(nvc["txtUserName"]))
{
  userName = nvc["txtUserName"];
}

if (!string.IsNullOrEmpty(nvc["txtPassword"]))
{
  password = nvc["txtPassword"];
}

There's an extra double quote character on your field ID and name values.

<td>
    <input type="text" id="txtName2″" name="name2″" value="Jack" />
</td>

Should be

<td>
    <input type="text" id="txtName2" name="name2" value="Jack" />
</td>

The same happens to your txtAddr2 field. As the saying goes, 'The Devil is in the detail'.

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