Sunday, July 29, 2012

Send multiple parameters in jquery ajax in asp.net


Here I am going explain how to send  multiple parameters using JQuery or JSON in asp.net.
If we want to send or pass multiple parameters using JSON or JQuery in asp.net we need to declare it like as shown below in our aspx page under <script> tag.
Let's write a simple code to understand it.


$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "SavecNetCode.aspx/SaveData",
data: JSON.stringify({title: 'value1' ,ID: 'value2' }),
dataType: "json",
success: function(data) {
$('lbltxt').text(data.d);
},
error: function(result) {
alert("error");
}
});
Here JSON.stringify is used to encode the parameters in JSON format and our webmethod will be like as shown below
C# Code
[WebMethod]
public static string SaveData(string title,string ID)
{
string str = ID+ title;
return str;
}


:))

18 comments:

  1. Great article, i just get very good jquery, i was looking for it from few days. Thanku for share this one with us.

    ReplyDelete
  2. It is really good to visit to your post guys as we can get new information about jquery, Thanks for this useful post.

    ReplyDelete
  3. It is really good to visit to your post guys as we can get new information about jquery, Thanks for this wonderful post.

    ReplyDelete
  4. Nice post, things explained in details. Thank You.

    ReplyDelete
  5. This site definitely explains essential concepts to its readers. Thanks for sharing..

    ReplyDelete
  6. Thanks a lot for writing this post.Thanks a lot for sharing. Keep blogging.

    ReplyDelete
  7. I believe that what I just read is some good material. Thanks for continuing to write such wonderful articles. God bless.

    ReplyDelete
  8. Extremely good article! I m a usual website visitor (very much like addict :P) to your website. Keep up the fantastic work .

    ReplyDelete
  9. Very informative, keep posting such good articles, it really helps to know about things :)

    ReplyDelete
  10. I guess I am not the only one having all the enjoyment here! Keep up the good work..

    ReplyDelete
  11. I support the writer's unique point. It is useful and benefit to your daily life.

    ReplyDelete
  12. That's what one may call great post. Seriously, I like it the way you described it and made it easy to understand.

    ReplyDelete
  13. Thanks…:)Just desire to say your article is as astounding.

    ReplyDelete
  14. Thank sharing very nice information about multiple parameters i like this article http://www.technologyexplores.com

    ReplyDelete
  15. Great article. Thanks for your great information, the content is quiet interesting. I will be waiting for your next post.

    ReplyDelete
  16. hi i am trying to do same thing but i am getting error, please help me out :
    Step 1: In my .js file below code i have placed:
    $('#pre_rev_con .submitBtn').on('click',function(){

    $.ajax({
    type: "POST",
    contentType:"application/json;charset=utf-8",
    url: "TestAjax.aspx/PopulateUserRegistration",
    data:JSON.stringify({fname: 'value1' ,lname: 'value2' }),
    dataType:'json',
    success: function (data) //what to do if succedded
    {
    alert(data.d);
    },
    error: function (jqXHR, textStatus, errorThrown) //what to do if fails
    {
    alert('bad, ' + errorThrown + ", " + jqXHR.responseText + ", " + textStatus);
    }
    });

    Step 2: .cs file as code as below:

    [System.Web.Services.WebMethod]
    [ScriptMethod(UseHttpGet = false)]
    public string PopulateUserRegistration(string fname, string lname)
    {
    return ("my name is " + fname + " " + lname);
    }

    but i am getting parsing Error, plrease help me out

    ReplyDelete
  17. I certainly agree to some points that you have discussed on this post. I appreciate that you have shared some reliable tips on this review.

    ReplyDelete