-1

I have the following setup

On client side I am using a complex Javascript variable something like this

var order = {
             name:"",
             id:"",
             cost:"",
             details:{
                      sItem:[{name:"",cost:""}], 
                      dItem:[{name:"",cost:"", components:[{name:"",quantity:""}]}]
                     }
            }

I have a controller on server in c# like this

    public string getCost(string order)
    {
        var sOrder = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(order);
//do the processing here
        return "I am processed";
    } 

In my Javascript I am calling this as

$.getJSON("Api/menu/getCost/" +  JSON.stringify(order),
                function (data) {
                    window.alert('i m here : success');
                });

The problem is when I send this request the server responds with bad request, however if I append a simple string like "hello" instead of JSON.stringify(order). The controller receives it and returns with success with no problem, so I know the problem is somewhere in converting the order to JSON but dont know how to find out.

Yes, I am using getJSON and returning a simple string but thats not a problem, as I will be returning a JSON string later on when the processing is done. Actually that will be the same JSON received with some values of the properties changed.

7
  • 1
    Don't you want to send the id instead of the full json ? You can't send json through URL, some characters might not be supported Commented Dec 13, 2016 at 2:25
  • In some cases there is no Id for the items inside the nested structure and therefore I need to send all the order object. Furthermore it will make it easy on both the server and client side if they just have a common object to deal with, partly filled by client and partly filled by the server. Commented Dec 13, 2016 at 2:36
  • Well I've got confused by DeserializeObject<dynamic>(id); which was supposing an ID. If you absolutely need to send the data, do a POST request instead of passing into URL. Commented Dec 13, 2016 at 2:39
  • oh yes, I have corrected it. Do you mean calling a postJSON instead of getJSON? Or something else Commented Dec 13, 2016 at 2:43
  • I hope the below stackoverflow post can help you. stackoverflow.com/questions/8232197/… Commented Dec 13, 2016 at 2:45

1 Answer 1

0

I played with $.get, $.post, $.getJSON, etc. While I got some of them to work, they always returned jqXHR even though the response was a serialized JSON object. Below is the only way I have been successful. (Note that I am hitting a web service.)

       <script type="text/javascript">

        $(document).ready(function () {

            $.ajax({
                url: "WebService1.asmx/GetAList",
             //   data: JSON.stringify({ s: parms }),
                type: 'post',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data, status) {
                    var contries = JSON.parse(data.d);
                    $("#sel2").select2({ data: contries });
                },
                error: function (one, two) {
                    debugger;
                }
            });
        });
            </script>


        </head>
        <body>
            <form>
                <div>
                    <p>populated from ajax</p>
                    <select style="width:175px" id="sel2"></select>
                </div>
            </form>
        </body>
</html>

and my web service

using System.Web.Script.Services;

    namespace WebApplication1
    {    // a class in context with how the data are is being used
        [Serializable]
        public class select2Item
        {
            public String id { get; set; }
            public String text { get; set; }
            public select2Item(String code, String name)
            {
                this.id = code;
                this.text = name;
            }
            public select2Item() { }
        }

        /// <summary>
        /// Summary description for WebService1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {


            [WebMethod]
            public string GetAList()
            {
                System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
                return makelist();
            }

             // return a list of serialized codes and countries
            public String makelist()
            {
                List<select2Item> list = new List<select2Item>();

                list.Add(new select2Item("AI", "Anguilla"));
                list.Add(new select2Item("AQ", "Antarctica"));
                list.Add(new select2Item("AG", "Antigua and Barbuda"));
                list.Add(new select2Item("AR", "Argentina"));
                list.Add(new select2Item("AM", "Armenia"));
                list.Add(new select2Item("AW", "Aruba"));
                list.Add(new select2Item("AU", "Australia"));
                list.Add(new select2Item("AT", "Austria"));
                list.Add(new select2Item("AZ", "Azerbaijan"));
                list.Add(new select2Item("BS", "Bahamas"));
                list.Add(new select2Item("BH", "Bahrain"));
                list.Add(new select2Item("BD", "Bangladesh"));
                list.Add(new select2Item("BB", "Barbados"));
                list.Add(new select2Item("BY", "Belarus"));
                list.Add(new select2Item("BE", "Belgium"));
                list.Add(new select2Item("BZ", "Belize"));
                // did it this way to show you which to use
                System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
                String jsonList = ser.Serialize(list);
                return jsonList;
            }
         }
    }
Sign up to request clarification or add additional context in comments.

Comments

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.