2

I want to post form data of asp.net using ajax. and want to receive that data on another page as form array.

this is want I am doing now Default2.aspx

 $('#btnSubmit').click(function () {
            $.ajax({
                type: "POST",
                url: "Default3.aspx",
                data:  $('#form1'),
                success: function (msg) {
                    alert("Success");
                }
            });
        });

and on Default3.aspx

 protected void Page_Load(object sender, EventArgs e)
    {

        int loop1;
        NameValueCollection coll;

        //Load Form variables into NameValueCollection variable.

        coll = Request.Form;
        // Get names of all forms into a string array.
        String[] arr1 = coll.AllKeys;
        for (   loop1 = 0; loop1 < arr1.Length; loop1++)
        {
            Response.Write("Form: " + arr1[loop1] + "<br>");
            Label1.Text = arr1[loop1];
        }

    }

Update : I am sending serialized object through ajax call . I want to that data in my asp.net code. How can i do that ?

3 Answers 3

5

Use the serialize method.

Since you are using a form you can let it control how to do the post:

var $frm = $('#form1');
$('input[type="submit]', $frm).click(function (e) {
    e.preventDefault();

    $.ajax({
        type: $frm.attr('method'),
        url: $frm.attr('action'),
        data:  $frm.serialize(),
        success: function (msg) {
            alert("Success");
        }
    });
});

Which can be refactored into a generic script that you can place in your masterpage:

<script type="text-javascript">
    $(function() {
        $('.post-using-ajax').each(function() {
            var $frm = $(this);
            $frm.submit(function (e) {
                e.preventDefault();

                $.ajax({
                    type: $frm.attr('method'),
                    url: $frm.attr('action'),
                    data:  $frm.serialize(),
                    success: function (msg) {
                        alert("Success");
                    }
                });
            });
        });
    });
</script>

Which let's you to transform all forms with the CSS class post-using-ajax into AJAX forms.

<form method="POST" action="someAction" class="post-using-ajax">
    <!-- all form items -->
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

@MurtazaHussain: What are you trying to say? Both parts of my example should work as you say. You might want to rephrase your question otherwise.
0

Check this once . Post data to other page using ajax

2 Comments

not helping. I making ajax call and want to receive data on the request page. I want to know that how I can receive data..
Consider adding the important content of the link to your answer. The link you have now doesn't directly answer the question, and instead links only to a project.
0

if there was few fields in your form, maybe you should use $.ajax like as :

$.ajax({
                type: "POST",
                url: "default3.aspx",
                data: {name:"usename", sex:"male", age:"18"},
                dataType:"json",
                success: function (msg) {
                    alert("Success");
                }
            });

i think this is the simplest way.

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.