0

First of all- I know I should use HTTP POST and I know how to do it. But my task is to use HTTP GET anyway. Now, there's a problem. I have a WebMethod:

[WebMethod]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json,UseHttpGet =true)]
        public List<string> AddToCollection(string name, string lastname)
        {
            collection.Add(name + " " + lastname);
            return collection;
        }

Now, code from .js :

function AddToArray() {
            var name = document.getElementById("name_add").value;
            var lastname = document.getElementById("surname_add").value;
            if (name == "" || lastname == "") {
                alert("Wrong!");
            } else {
                var dataT = JSON.stringify({"name":name,"lastname":lastname});
                $.ajax({
                    type: "GET",
                    url: "http://localhost:45250/ServiceJava.asmx/AddToCollection",
                    data: dataT,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        //something
                    }
                })
            }            
        }

Now the thing is: it works perfect when I use type POST. But with GET I get the error that the parameter name cannot be recognized. What should I change to make it work?

Paweł Poręba
  • 1,084
  • 1
  • 14
  • 37
  • 1
    I think you'll need to add your data to the url as query strings. Not sure though – Akshay Khandelwal Nov 21 '15 at 17:05
  • That helped me to solve a problem, later I'll post the code. But isn't it possible to use `GET` and pass `JSON` data? – Paweł Poręba Nov 21 '15 at 18:11
  • The HTTP spec does not allow a BODY on a GET request. Only POST and PUT can have bodies. – Eris Nov 21 '15 at 18:13
  • Possible duplicate of [HTTP GET with request body](http://stackoverflow.com/questions/978061/http-get-with-request-body) – Eris Nov 21 '15 at 18:15
  • Well, relying on the post you gave the link to, it is allowed a BODY on a GET request, but it's not usefull and violating some HTTP principles. – Paweł Poręba Nov 21 '15 at 18:33

0 Answers0