0

I have a simple jQuery AJAX method that send data to a web method which brings data from da database. When I send number it works properly but I don't know how to send data with parameter.

For example this method works properly:

function catchdata() {
     $.ajax({
         type: "POST",
         url: "rank.aspx/bringdata",
         data: "{lmt:16}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         async: true,
         cache: false,
         success: function(ret){
             s = ret.d;
             dos(s, 0);
         },
         error: function(x,e){
             alert("error occur");
         } 
     });
}

But this below code does not work and error function raise:

function catchdata() {
     $.ajax({
         type: "POST",
         url: "rank.aspx/bringdata",
         data: {
             lmt:total
         },
         async: true,
         cache: false,
         success: function(ret){
             s = ret.d;
             dos(s, 0);
         },
         error: function(x,e){
             alert("error occur");
         }
     });
}
5
  • plese helpe me in parameterise json dta in jquery ajax method .thanks Commented Aug 8, 2012 at 8:40
  • where do you get total from? have you tried alerting it for check before the $.ajax? Commented Aug 8, 2012 at 8:40
  • nono asp function work properly im sure. my problem is in ajax method cuz when i send data without parameter for example number 16 or 20 and etc its ok .but i cant work with parameter Commented Aug 8, 2012 at 8:43
  • $.post("test.php", { name: "John", time: "2pm" } ); Commented Aug 8, 2012 at 8:44
  • Is the aspx page expecting a posted Json string to parse or is it expecting posted form varibles ? First example does the former second does the latter, with this being an aspx page rather than an WCF service it wont try to do Json behavours automatically. I'd Question why this is a page at all if its a service you want then use a WCF service if you dont need somthing that complex use a HttpHandler ? Commented Aug 8, 2012 at 8:51

1 Answer 1

3

If the first example works fine, this should do the same with a parameter: (I assume total is defined as global variable elsewhere)

$.ajax({
     type: "POST",
     url: "rank.aspx/bringdata",
     data: "{lmt:" + total + "}",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     async: true,
     cache: false,
     success: function(ret){
         s = ret.d;
         dos(s, 0);
     },
     error: function(x,e){
         alert("error occur");
     } 
 });
Sign up to request clarification or add additional context in comments.

4 Comments

but shouldn't it work with {lmt:total} too? What's the problem there?
@Onheiron as also explained in this comment, when passing data as JSON it should be a raw string as it's parsed on the server. When passing as POST it should be array and jQuery is sending them properly behind the scenes.
But I used before something like $.post('someaddress.php',myJSO,function(data){alert(data)}) where myJSO was some like {key1:'value1',key2:'value2' ...} just like a JavaScript Object and it worked fine. Also the comment actually says that with that contentType set the behaviour would be the one you described, but this doesn't mean the data: must always be a raw string
@Onheiron not always, but when you pass it as JSON it should be.

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.