views:

104

answers:

3

I am using the following scrip to post data into ASP.Net

$.post(window.location, { name: "John", time: "2pm" })

In my page_load event , I am checking Request.Forms.AllKeys but the count is comming as zero.

My form is <form name="aspnetForm" method="post" action="Default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm">

+2  A: 

$.post is a AJAX call by jQuery. It has nothing to do with the post on your form.

You can use a PageMethod to achieve what you are trying:

Create in your codebehind something like

[WebMethod]
public static void HandleMyPost(string name, string time)
{
    //do something

}

Then add a ScriptManager control to your .aspx page, and set EnablePageMethods="true".

Then call your method from JavaScript (where your $.post is now) through

PageMethods.HandleMyPost(function() {}, "John", "2pm")
Jan Jongboom
No one should have to use ASP.NET-specific technologies like `PageMethods` to produce a simple AJAX post.
Jeff Sternal
Well he is now doing a new page request to his current page, and doing a whole new page cycle, which is way worse than just using a PageMethod. PageMethods are just the most easy way to create an AJAX call in ASP.Net, you can use a real `jQuery.post` by calling the page using `pagename.aspx/handlemypost/`; so you don't have to stick to ASP.Net ajax. This is just the most easy to implement, convenient solution.
Jan Jongboom
@Jan - you're absolutely right. I kind of missed the forest for the trees there.
Jeff Sternal
A: 

That code should work. Here's what i did to test

In my code behind i have:

protected void Page_Load(object sender, EventArgs e)
{
    foreach (var key in Request.Form.AllKeys)
    {
        // do stuff here.
    }
}

On the page i have:

<script type="text/javascript">
    $(document).ready(function()
    {
        $('#btn').click(function()
        {
            $.post(window.location, { name: "John", time: "2pm" });
            return false;
        });
    });
</script>


<input type="button" id="btn" value="Click Me" />

screenshot

When i click the button and have a breakpoint on the foreach i can see the post values, there are two.

John Boker
@John, which version of jQuery are you using?
Jeff Sternal
v1.3.2 is the version im using.
John Boker
Strange - I thought our differing results might be due to multiple versions of jQuery. When I used `window.location` this failed for me on IE 6.0 and Firefox 3.5.6 using jQuery 1.3.2.. When I changed to `location.href` (or `location.pathname`) it worked fine. What browser(s) are you using?
Jeff Sternal
I was just debugging with IE8. location.href sounds like a better choice though. I didnt have anything on the query string at the time.
John Boker
A: 

The window.location object isn't a valid [url] argument for jQuery.post. You should use window.location.href instead:

// or the shorter location.href
$.post(location.href, { name: "John", time: "2pm" })

However, instead of failing, jQuery 1.3.2 (at least) falls back to its default ajaxSettings, which are (among other things):

ajaxSettings: {
    url: location.href,
    type: "GET"
}

Since it's issuing a GET request, you obviously won't see your data in the Request.Form collection.

Jeff Sternal