开发者

issue to send text using jquery's $.ajax() and ASP.Net

开发者 https://www.devze.com 2023-01-04 01:13 出处:网络
I use to work with the animation on jquery but never ajax, so it\'s m开发者_如何转开发y first experience with $.ajax() function of jquery:

I use to work with the animation on jquery but never ajax, so it's m开发者_如何转开发y 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


Try to change the data like this:

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

instead of:

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

精彩评论

暂无评论...
验证码 换一张
取 消