1

i am learning Jquery Ajax method. i try to post one json string , it working using $.post method but not working in $.Ajax method . it is giving 500 error.please give some suggession

---- $.post--- method // working

   $.post("About.aspx?type=Test", { 'Data': '[{"Key":"454","Value":["1","3"]},{"Key":"496","Value":["1","2","3"]}]' }, function (result) {
                alert(result.d);
            });
   ----c#-----
    public void add()
    {
        string value = Request.Form["Data"];
    }

------$.Ajax post--------- method // Not working. but if i am passing data as "{'data':'1'}"--working

      $.ajax({
                type: "POST",
                url: "Contact.aspx/add",
                dataType: "json",
                data:  "{'Data': '[{'Key':'454','Value':['1','3']},{'Key':'496','Value':['1','2','3']}]'}",
                contentType: "application/json; charset=utf-8",
                success: function (response) {

                },

                error: function (msg) {
                    alert(msg.status);
                }
            });
  -----c#----
   [WebMethod]
    public static void add( string Data)
    {
    }
nichu09
  • 882
  • 2
  • 15
  • 44
  • the `data` value in your `$.ajax` request is not JSON, it's just a string. and why are you trying to escape all the double quotes with a backslash? – low_rents Oct 14 '15 at 08:58
  • dont escape the `data`, just enclose the entire data with single quotes `'` – dreamweiver Oct 14 '15 at 09:09
  • @dreamweiver single quotes or double quotes don't matter in javascript/json. and no - he should NOT enclose the entire data with quotes at all, because then it is a string. – low_rents Oct 14 '15 at 09:11

1 Answers1

1

update:

I maybe misunderstood what you wanted - so if you really want to send the json to the server as a string, then your data should look like this:

data: {
    roleList : JSON.stringify([{"Key":"454","Value":["1","3"]},{"Key":"496","Value":["1","2","3"]}])
},

seems like you want roleList as your POST-variable name; and it's so much easier to use the build-in function JSON.stringify() here. nearly every browser got it nowadays.

additional info about the contentType:
the contentType is giving me weird issues. seems like you are better off not using it at all since you should be fine with the default setting. see this question for further information: Cannot set content-type to 'application/json' in jQuery.ajax

original answer:
the JSON you had as your data value was not valid JSON but just a string. plus the contentType was invalid. you are fine with not setting the contentType at all and just using the default setting:

$.ajax({
    type: "POST",
    url: "Contact.aspx/add",
    dataType: "json",
    data: {roleList:[{"Key":"454","Value":["1","3"]},{"Key":"496","Value":["1","2","3"]}]},
    //contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log(response);
    },

    error: function (msg) {
        console.log("error: ", msg.status);
    }
});
Community
  • 1
  • 1
low_rents
  • 4,481
  • 3
  • 27
  • 55
  • if i don't specify the content type ,it is not hitting the web method. string parameter is in the method . so i convert javascript object to string using JSON.stringify method and passing . if i passed just one data like "{'data' :'1'}" is working . but this is not working – nichu09 Oct 14 '15 at 09:40
  • @nichu09 please see my updated answer. try my JSON.stringify version and tell me if it works for you (for me it does - but i got PHP on the server side) – low_rents Oct 14 '15 at 09:44
  • JSON.stringify using convert javascript object to string. that ouput i passed as string . if i passed as javascript object , you can't able to see the value. so i did this line of code before and pass the value.but using content type {'data' :'1'}" is working. so why not this one . help appreciated – nichu09 Oct 14 '15 at 10:03