views:

197

answers:

4

Hello

I have an ASP.NET application in which i want to post data using jQuery to another page. It means i want post the data of page.

How can i do this with jQuery or AJAX?

Please help me.

 $(document).ready(function() {
         alert("start");
        $("#btnSave").click(function() {
        alert("start1");
            var aa = 'bb';
            var json = "{'ItemName':'" + aa + "'}";
            alert("start2");
            var ajaxPage = "Default3.aspx?Save=1"; //this page is where data is to be retrieved and processed
            alert("start3");
            var options = {
                type: "POST",
                url: ajaxPage, 
                data: json,
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: false,
                success: function(response) {
                    alert("success: " + response);
                },
                error: function(msg) { alert("failed: " + msg); }

            };

alert("start4");

        });



    });

I am using this code I am getting all alert response but its posting page.

A: 

There is load function.

You may use it like this:

$('#somediv').load('http://someaddress',{key:value}, function callback(){});

Second parameter is important - only written in this way performs post.(if you pass array then it performs get)

PS> This is good when you want to read the data, if you want to just do the POST and don't care about what comes back then you may want to use: http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype

kubal5003
A: 

Look at $.post() - http://docs.jquery.com/Ajax/jQuery.post

Add all your relevant data, and post away, handling the response with the supplied callback method.

Marcus
A: 

There is a post function in jQuery:
$.post("test.php", { name: "John", time: "2pm" } );
This is copied directly from the jQuery API, so for more details and examples you can look there.
jQuery api
choose Ajax > Ajax Requests > $.post

Jeroen Pelgrims
+1  A: 

Jquery and JSON works great with ASP.NET. You can call a code behind method directly from javascript and return complex objects, not just string. (for this example to work you need json2.js found here http://json.org/json2.js)

//javascript
function postMethod(text){ 
var jsonText = JSON.stringify({ name:text });
    $.ajax({
    type: "POST",
    url: "yourpage.aspx/GetPerson",
    contentType: "application/json; charset=utf-8",
    data: jsonText,
    dataType: "json",
    success: function(response) {
        var person = response.d;
        alert(person.Name);
    }
});
}

//aspx code behind
[WebMethod]
public static Person GetPerson(string name)
{
   Person person = new Person(name);
   return person;       
}
Martin Larsson
Thanks for reply but its not working,Its not post any given page.can you explain me this please i need this urgent.
Pankaj Mishra
$ not define error i am getting
Pankaj Mishra
$.ajax({ type: "POST", url: "yourpage.aspx/GetPerson", contentType: "application/json; charset=utf-8", data: jsonText, dataType: "json", success: function(response) { var person = response.d; alert(person.Name); }this part is not working
Pankaj Mishra
It's jquery, have you included the jquery file? Try putting the javascript file on the same aspx page you are calling, Default3.aspx in your example. As an alternative you can call an webservice instead and have the response method there.
Martin Larsson