3

My knowledge of ajax and JSON is limited, but I know that using JSON.stringify in an ajax call can sometimes be useful. I have an ajax call below that works fine, while the one below it with the stringify method that does not work. I am wondering if I am using .stringify correctly, and if not, when should I use JSON.stringify in ajax, if ever. I am using MVS with a model, view, and controller.

This is how I usually do ajax calls, and how i build the url portion.

    function AddEquipment(id, name, type, description, email) {
        $.ajax({
            url: '@Url.Action("AddEquipment", "Home")' + '/?id=' + id +
                "&name=" + name + "&type=" + type + "&description=" +
                description + "&email=" + email,
            type: "GET",
            cache: false,
            datatype: "JSON",
            success: function(result) {
                //do stuff
            }
        });
    }

Below I have tried using JSON.stringify instead of building the entire url manually, and it does not work.

    function AddEquipment(id, name, type, description, email) {
        $.ajax({
            url: '@Url.Action("AddEquipment", "Home")',
            type: "GET",
            cache: false,
            datatype: "JSON",
            data: JSON.stringify({
                "id": id,
                "name": name,
                "type": type,
                "description": description,
                "email": email
            }),
            success: function(result) {
                //do stuff
            }
        });
    }

the controller method this goes with accepts id as an int, while everything else is a string. I have used JSON.stringify before with mixed variables (ints, bools, strings) without an issue.

Any helpful information is greatly appreciated,

Thanks!

Jacobalo
  • 233
  • 1
  • 4
  • 13
  • 3
    I believe jQuery does the heavy lifting for you ... just pass data as an object - read the [documentation](http://api.jquery.com/jquery.ajax/) for more – Jaromanda X Nov 21 '16 at 22:06
  • Have you opened the developer tools and looked at the "Network" tab to see what the URL of the AJAX call actually winds up being with this JSON code? – Scott Marcus Nov 21 '16 at 22:08
  • JSON would normally be sent via the POST method, and then the requests body will have the data. If you want to send via a query param, you would want to stringify and then use encodeURIComponent on it too. And then you can do the reverse at the server end. – Keith Nov 21 '16 at 22:11
  • 1
    In this case, don't use `JSON.stringify` at all. If you pass `data` as an object, jQuery will convert to a query string properly. If you pass a string, such as JSON, then it is expected that the string is already in the proper `key=value&k2=v2...` format, not a JSON string. – Jonathan Kuhn Nov 21 '16 at 22:16

1 Answers1

5

These are two different strings (values that they eventually evaluate to be). One is not equal to other. Stringify will not yield you ' = ' as you require.

Read this post to pass data on a get call

JSON.stringify({
                "id": id,
                "name": name,
                "type": type,
                "description": description,
                "email": email
            }),

url: '@Url.Action("AddEquipment", "Home")' + '/?id=' + id +
                "&name=" + name + "&type=" + type + "&description=" +
                description + "&email=" + email 
Community
  • 1
  • 1
Winter Soldier
  • 2,607
  • 3
  • 14
  • 18