views:

45

answers:

2

Hello everybody, i use to work with the animation on jquery but never ajax, so it's my first experience with $.ajax() function of jquery:

i am trying to make my web application only using jquert library, i checked over the web for a good tutorial to make asp.net and jquery work, and i found an walkthrought, in this article they send a int to an WebMethod and it work for me, but in my case i want to send String and/or objects.

i know the issue is int the datatype and/or contentType. here is my code sample.

    <script type="text/javascript" src="scripts/jquery-1.4.1.min.js"></script>
    <script language="javascript">

    $(document).ready(function() {
        $("#name").change(function() {
            var myname = this.value;
            var options = {
                type: "POST",
                url: "dollarajax.aspx/hello",
                data: "{nom:" + myname + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    if (response.d != "") {
                        alert(response.d);
                        $("#name").focus();
                    }
                }
            };

            $.ajax(options);
        });

    });
</script>

[WebMethod]
public static string hello(String nom)
{        
    return ("hello my friend: " + nom);
}

so any idea or any document where i can figure the trick to make it work? thanks

A: 

Why you use Json as datatype? Use simple text. Like:

dataType: text

and:

data: myname
ieaglle
+1  A: 

Try to change the data like this:

data: '{"nom":"'+ myname +'"}',

instead of:

data: "{nom:" + myname + "}", 
Mark Schultheiss
but that mean that i have to parse every single parametre to it format in the back end.thanks it's working!
aleo
You can also add the JSON parser from http://www.json.org/json2.js which has a stringify method for this if it is NOT in your browser.Here is the page with a minified version as well: http://www.json.org/js.html
Mark Schultheiss