2

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.

1
  • the ajaxPage variable should look like this "Default3.aspx/Save". And then in you code behind on that page have [WebMethod] public static string Save(string ItemName){ //do stuff here } Try putting the javascript on the default3.aspx page or use a webservice instead. also remeber to include using System.Web.Services; in the code behind Commented Nov 3, 2009 at 22:49

4 Answers 4

2

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 https://github.com/douglascrockford/JSON-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;       
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply but its not working, Its not post any given page. can you explain me this please i need this urgent.
$.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
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.
0

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

Comments

0

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.

Comments

0

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

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.