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!

4
  • 3
    I believe jQuery does the heavy lifting for you ... just pass data as an object - read the documentation for more Commented Nov 21, 2016 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? Commented Nov 21, 2016 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. Commented Nov 21, 2016 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. Commented Nov 21, 2016 at 22:16

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, they're not objects at all.
@Bergi Yes, strings rather! :) I'm talking about the url(whatever that it ends up to be) & the JSON.stringify
If you've found a question which is the same as the current one, you can click the flag link below the question and mark it as a duplicate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.