2

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">

3 Answers 3

2

$.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")
Sign up to request clarification or add additional context in comments.

3 Comments

No one should have to use ASP.NET-specific technologies like PageMethods to produce a simple AJAX post.
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 - you're absolutely right. I kind of missed the forest for the trees there.
1

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 http://imagebin.antiyes.com/images/0565978001261663525_33.jpg

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

4 Comments

@John, which version of jQuery are you using?
v1.3.2 is the version im using.
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?
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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.